Swift 之 UIAlertView

这篇博客介绍了在Swift中如何使用UIAlertView(已弃用)和UIAlertController来创建警告对话框。展示了多种创建UIAlertView的方式,并详细解释了其相关属性和方法。接着转向UIAlertController,讲解了如何创建带或不带输入框的警告对话框,以及添加操作和处理用户输入的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UIAlertView 可以使用,但是已被苹果弃用,创建UIAlertView有好几种不同的方式:

1、  let  alterview = UIAlertView.init(title: "系统提示", message: "Hello World", delegate: self, cancelButtonTitle: "确定")
        
        self.view.addSubview(alterview)
        
        alterview.show()

2、   let  alterview = UIAlertView.init(title: "系统", message: "Hello", delegate: self, cancelButtonTitle: "确定", otherButtonTitles: "取消","其他")  //其他可以去到也可以后面再添加字符串
        
        self.view.addSubview(alterview)
        
        alterview.show()

3、   let  alterview = UIAlertView()
        alterview.title = "系统消息"
        alterview.message = "Hello"
        alterview.addButton(withTitle: "取消")
        alterview.addButton(withTitle: "确认")
        alterview.cancelButtonIndex = 0
        alterview.delegate = self
        self.view.addSubview(alterview)
        alterview.show()


相关属性

 样式:alterview.alertViewStyle = UIAlertViewStyle.loginAndPasswordInput  带有两个输入框

            alterview.alertViewStyle = UIAlertViewStyle.plainTextInput    带有一个输入框

            alterview.alertViewStyle = UIAlertViewStyle.secureTextInput  带有一个输入框,输入是隐藏的密码类型

            alterview.alertViewStyle = UIAlertViewStyle.default  默认样式

方法:

   func willPresent(_ alertView: UIAlertView) {
        //警告框显示前别调用
    }
    
    func didPresent(_ alertView: UIAlertView) {
        //警告框显示后被调用
    }
    func alertViewCancel(_ alertView: UIAlertView) {
        //警告框显示中强制关闭时被调用,例如警告框显示时应用程序突然关闭等场合 触摸取消按钮时不会调用此方法
    }
    func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
        //触摸警告框中任一按钮时被调用,此方法比alterview:willDismissWithButtonIndex 方法先被调用
        if buttonIndex == alertView.cancelButtonIndex {
            print("点击了取消")
        }else{
            print("点击了确认")
             // 如果里面包含输入框,获取输入框中的信息
            let name = alertView.textField(at: 0)
        }
    }
    func alertView(_ alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int) {
        //警告框关闭前调用
    }
    func alertView(_ alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) {
        //警告框关闭后被调用,警告框显示中应用程序进入睡眠状态时也会被调用
    }
  



UIAlertController

//制作demo的时候再好放在其他控件里边调用,如果是直接写在viewDidLoad()有时候失效

        let alterController = UIAlertController.init(title: "系统消息", message: "hello", preferredStyle: UIAlertControllerStyle.alert)
        let cancleAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.cancel, handler: nil)
        let okAction =  UIAlertAction.init(title: "确认", style: UIAlertActionStyle.default, handler: nil)
        
        alterController.addAction(cancleAction)
        alterController.addAction(okAction)
      
        self.present(alterController, animated: true, completion: nil)
        
带有输入框的

    let alterController = UIAlertController.init(title: "系统消息", message: "hello", preferredStyle: UIAlertControllerStyle.alert)
        let cancleAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.cancel, handler: nil)
        alterController.addTextField { (textField:UITextField!) in
            textField.placeholder =  "登录"
        }
        
        alterController.addTextField {
            
            (textField: UITextField!) -> Void in
            
            textField.placeholder = "密码"
            
            textField.isSecureTextEntry = true
            
        }
        
        
        let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.default) {
            
            (action: UIAlertAction!) -> Void in
            
            var login = (alterController.textFields?.first)! as UITextField
            
            var password = (alterController.textFields?.last)! as UITextField

         //处理事件在这里           
        }

        
        alterController.addAction(cancleAction)
        alterController.addAction(okAction)
        
        self.present(alterController, animated: true, completion: nil)







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值