Swift完成UIAlertController的调用 -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

    iOS8中的UIAlertView和UIActionSheet已经都被UIAlertViewController代替了,所以,本篇blog就来探讨下如何用swift生成提示框,

Swift完成UIAlertController的调用

    我们先来看一下Apple的UIAlertController的文档:

   

import Foundationimport UIKit////  UIAlertController.h//  UIKit////  Copyright (c) 2014 Apple Inc. All rights reserved.//@availability(iOS, introduced=8.0)enum UIAlertActionStyle. Int {        case Default    case Cancel    case Destructive}@availability(iOS, introduced=8.0)enum UIAlertControllerStyle. Int {        case ActionSheet    case Alert}@availability(iOS, introduced=8.0)class UIAlertAction : NSObject, NSCopying {        convenience init(title: String, style. UIAlertActionStyle, handler: ((UIAlertAction!) ->Void)!)        var title: String { get }    var style. UIAlertActionStyle. { get }    var enabled: Bool}@availability(iOS, introduced=8.0)class UIAlertController : UIViewController {        convenience init(title: String?, message: String?, preferredStyle. UIAlertControllerStyle)        func addAction(action: UIAlertAction)    var actions: [AnyObject] { get }    func addTextFieldWithConfigurationHandler(configurationHandler: ((UITextField!) ->Void)!)    var textFields: [AnyObject]? { get }        var title: String?    var message: String?        var preferredStyle. UIAlertControllerStyle. { get }}

    我们可以看到UIAlertController的style有两个,一个是ActionSheet,一个是Alert,而AlertActionStyle有3个: Default,Cancel, Destructive;所以我们新建Alert时可以这样:

   

var alert: UIAlertController = UIAlertController(title:nil, message:"您输入的电话号码有误,请检查后重新输入",                preferredStyle.:UIAlertControllerStyle.Alert)

    或者

   

var alert: UIAlertController = UIAlertController(title: nil, message:"test", preferredStyle. UIAlertControllerStyle.ActionSheet)

接下来我们来给Alert添加action,从文档中可以看到AlertAction有init函数,

    我们来新建3个actions

   

var saveAction = UIAlertAction(title: "Save", style. .Default, handler:{                    (alerts: UIAlertAction!) ->Void in                    println("File saved")                })                var deleteAction = UIAlertAction(title: "Delete", style. .Default, handler:{                    (alerts: UIAlertAction!) ->Void in                    println("File delete")                })                var cancelAction = UIAlertAction(title: "Cancel", style. .Cancel, handler:{                    (alerts: UIAlertAction!) ->Void in                    println("Cancelled")                })
注意到handler中用到了一个closure

    然后给我们的alertcontroller添加actions,并把它显示出来

   

alert.addAction(saveAction)                alert.addAction(deleteAction)                alert.addAction(cancelAction)                                self.presentViewController(alert, animated: true, completion: nil)

    我们也可以这样添加action

   

alert.addAction(UIAlertAction(title: "确定", style. .Destructive, handler: {                action in switch action.style{                case .Default:                    println("ok")                case .Cancel:                    println("cancel")                case .Destructive:                    println("Destructive")                    }                }            ))
接下来运行一下看看我们的alertController是什么样子的吧,

电脑资料

Swift完成UIAlertController的调用》(https://www.unjs.com)。

    Tips:

    如果style是cancel 那么字体会变粗;如果是destructive,字体会显示红色。

最新文章