美文网首页Swift编程
Swift基础之弹框的应用

Swift基础之弹框的应用

作者: 大脸猫121 | 来源:发表于2016-06-15 14:38 被阅读834次

    弹框有很多中样式,比如系统的中间弹框,底部弹框还有第三方的MBProgressHuD弹框等等,在本篇文章中小编将带大家一起来探讨一下多种多样的弹框效果。

    一、UIAlertController

    1.警告框-中间弹框

    自IOS8起,建议使用UIAlertController。

       override func viewDidAppear(animated: Bool) {
           let alertController = UIAlertController(title: "系统提示", message: "您确定要离开吗", preferredStyle: UIAlertControllerStyle.Alert)
           let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil )
           let okAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default) { (ACTION) in
               print("确定")
           }
           alertController.addAction(cancelAction);
           alertController.addAction(okAction);
           self.presentViewController(alertController, animated: true, completion: nil)
       } 
    

    效果图如下:

    普通警告框.png
    2.使用输入框和密码框的告警框
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated);
        
        let alertController = UIAlertController(title: "系统登录", message: "请输入用户名和密码", preferredStyle: UIAlertControllerStyle.Alert);
        alertController.addTextFieldWithConfigurationHandler { (textField:UITextField!) -> Void in
            textField.placeholder = "用户名";
        }
        alertController.addTextFieldWithConfigurationHandler { (textField:UITextField!) -> Void in
            textField.placeholder = "密码";
            textField.secureTextEntry = true;
        }
        let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil )
        let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Default) { (ACTION) -> Void in
            let login = alertController.textFields!.first! as UITextField
            let passWord = alertController.textFields!.last! as UITextField
            print("用户名:\(login.text) 密码:\(passWord.text)");
        }
        alertController.addAction(cancelAction);
        alertController.addAction(okAction);
        self.presentViewController(alertController, animated: true, completion: nil)
    }
    

    效果图如下:


    使用输入框和密码框的告警框.png
    3.底部弹框

    自iOS8起,开始用UIAlertController来代替UIActionSheet

     override func viewDidAppear(animated: Bool) {
        let alertController = UIAlertController(title: "系统提示", message: "您确定要删除吗", preferredStyle: .ActionSheet)
        let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
        let deleteAction = UIAlertAction(title: "删除", style: .Destructive, handler: nil)
        let archiveAction = UIAlertAction(title: "保存", style: .Default, handler: nil)
        alertController.addAction(cancelAction)
        alertController.addAction(deleteAction)
        alertController.addAction(archiveAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }
    
    底部弹框.png
    关于弹框就先介绍到这里,第三方的MBProgressHuD弹框以后会在小demo中使用,到时候再详细说明,在下一篇文章中小编会开始介绍swift中的导航控制器,请小伙伴们关注哦。还有就是,大家千万不要吝啬你们的指教哦。( _ )/~~拜拜

    相关文章

      网友评论

        本文标题:Swift基础之弹框的应用

        本文链接:https://www.haomeiwen.com/subject/kvfpdttx.html