UIKit框架-高级控件Swift版本: 6.UIAlertView方法/属性详解

来源:互联网 发布:仿真教学软件 编辑:程序博客网 时间:2024/06/05 08:54

前面我们学习了 UITextView, 现在让我们继续往下学.


1.UIAlertView的常用属性

// 1.设置 UIAlertView 的代理对象    var delegate: AnyObject?// 2.设置 UIAlertView 的标题名称var title: String// 3.设置 UIAlertView 信息内容var message: String?// 4.读取 UIAlertView 的按钮数量var numberOfButtons: Int { get }// 5.设置 UIAlertView 的取消按钮索引var cancelButtonIndex: Int// 6.读取 UIAlertView 的其他按钮索引var firstOtherButtonIndex: Int { get }// 7.读取 UIAlerView 是否显示var visible: Bool { get }// 8.设置 UIAlertView 的样式var alertViewStyle: UIAlertViewStyle

UIAlertView的样式

enum UIAlertViewStyle : Int {       case Default // 1.默认样式    case SecureTextInput // 2.密码输入样式    case PlainTextInput // 3.文本框输入样式    case LoginAndPasswordInput // 4.登陆输入样式}

2.UIAlertView的常用方法

// 1.添加 UIAlertView 的按钮标题func addButtonWithTitle(title: String) -> Int// 2.设置 UIAlertView 按钮标题的索引func buttonTitleAtIndex(buttonIndex: Int) -> String!// 3.显示 UIAlertViewfunc show()// 4.设置 UIAlertView 消失的按钮索引, 以及是否使用动画func dismissWithClickedButtonIndex(buttonIndex: Int, animated: Bool)// 5.设置 UIAlertView 的 TextField 的索引func textFieldAtIndex(textFieldIndex: Int) -> UITextField?

3.UIAlertView的代理方法

// 1.该方法是在 UIAlertView 上的按钮被点击时调用的    optional func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int)// 2.该方法是在 UIAlertView 将要被取消时调用    optional func alertViewCancel(alertView: UIAlertView)// 3.该方法是在 UIAlertView 即将显示的时候调用的    optional func willPresentAlertView(alertView: UIAlertView)// 4.该方法是在 UIAlertView 完全显示的时候调用的    optional func didPresentAlertView(alertView: UIAlertView)// 5.该方法是在 UIAlertView 即将消失的时候调用的    optional func alertView(alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int)// 6.该方法是在 UIAlertView 完全消失的时候调用的    optional func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int)// 7.该方法是在 UIAlertView 是否可以在任何一个编辑样式编辑时调用    optional func alertViewShouldEnableFirstOtherButton(alertView: UIAlertView) -> Bool

4.代码演示

首先要遵守代理协议

class ViewController: UIViewController, UIAlertViewDelegate {}

自定义一个UIButton并且监听 UIAlertView 的方法

    func myButton() {        var button: UIButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton        button.frame = CGRectMake(100, 200, 50, 20)        button.setTitle("弹窗", forState: UIControlState.Normal)        button.backgroundColor = UIColor.redColor()        button.addTarget(self, action: "myAlertView", forControlEvents: UIControlEvents.TouchUpInside)        self.view.addSubview(button)    }

自定义UIAlertView

    func myAlertView() {        // 1.自定义 UIAlertView, 并且初始化里面的 Title,Message, Delegate, CancelButtonTitle, OtherButtonTitles 等信息        var alertView = UIAlertView(title: "UIAlertView", message: "", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "确认")        // 2.继续添加 UIAlertView 的 ButtonTitle        alertView.addButtonWithTitle("添加")        // 3.设置 UIAlertView 的样式        alertView.alertViewStyle = UIAlertViewStyle.PlainTextInput        // 4.设置 UIAlertView 的取消按钮索引        alertView.cancelButtonIndex = 0        // 5.设置 UIAlerView 手动消失的按钮索引, 以及是否使用动画        alertView.dismissWithClickedButtonIndex(0, animated: true)        // 6.设置 UIAlerView 的输入文本框的索引路径        alertView.textFieldAtIndex(0)        // 7.设置 UIAlertView 的按钮标题索引        alertView.buttonTitleAtIndex(0)        // 8.显示 UIAlertView        alertView.show()    }

实现代理方法

    // 1.该方法是在 UIAlertView 上的按钮被点击时调用的    func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {        println("点击了AlertView")    }    // 2.该方法是在 UIAlertView 即将显示的时候调用的    func willPresentAlertView(alertView: UIAlertView) {        println("AlertView即将显示")    }    // 3.该方法是在 UIAlertView 完全显示的时候调用的    func didPresentAlertView(alertView: UIAlertView) {        println("AlertView已经完全显示")    }    // 4.该方法是在 UIAlertView 完全消失的时候调用的    func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) {        println("AlertView消失了")    }    // 5.该方法是在 UIAlertView 即将消失的时候调用的    func alertView(alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int) {        println("AlertView即将消失")    }    // 6.该方法是在 UIAlertView 将要被取消时调用    func alertViewCancel(alertView: UIAlertView) {        println("点击了取消按钮")    }    // 7.该方法是在 UIAlertView 在任何一个编辑样式编辑时调用    func alertViewShouldEnableFirstOtherButton(alertView: UIAlertView) -> Bool {        return true    }

在 viewDidLoad 中实现

    override func viewDidLoad() {        super.viewDidLoad()        self.myButton()    }

5.最终效果

1

2

PS:UIAlertView 是继承与 UIView 的, 所以 UIView 里的方法 UIAlertView 也可以使用


好了, 这次我们就讲到这里, 下次我们继续

0 0