加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 02:14:39 所属栏目:百科 来源:网络整理
导读:前面我们学习了 UITextView,现在让我们继续往下学. 1.UIAlertView的常用属性 // 1.设置 UIAlertView 的代理对象 var delegate : AnyObject? // 2.设置 UIAlertView 的标题名称 var title: String // 3.设置 UIAlertView 信息内容 var message: String? // 4.

前面我们学习了 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.显示 UIAlertView
func 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.最终效果

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


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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读