Swift基础之弹框的应用

弹框有很多中样式,比如系统的中间弹框,底部弹框还有第三方的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中的导航控制器,请小伙伴们关注哦。还有就是,大家千万不要吝啬你们的指教哦。( _ )/~~拜拜

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,271评论 4 61
  • 不要太把一个人当回事儿,因为-你可能在她心中什么也不是……
    梅听雪阅读 90评论 0 1
  • 最近发现自己有一些奇特的想法,比如想写文章,练书法,看散文,坐公交听音乐,喜欢一个人就想快点让他认识我. ...
    宋小鹤阅读 555评论 0 0
  • “小楠,我一个人走了,怕你跟着我会成为拖累,我随意惯了,逃票去了南方,听说南方有很多姑娘,到时候带回来给你看哈。”...
    安年_阅读 155评论 0 1
  • 天空飘着春雪,走进某小区,看到雪落在花上。看似真花,却闻不见花香。也许这是真花,也许这是假花。判断是真是假,观色...
    马得清阅读 718评论 0 0