美文网首页Swift
swift-UIAlertController

swift-UIAlertController

作者: Coder东 | 来源:发表于2017-03-30 15:52 被阅读274次
    //
    //  ViewController.swift
    //  Swift-普通警告框的使用
    //
    //  Created by 品德信息 on 2016/12/28.
    //  Copyright © 2016年 品德信息. All rights reserved.
    //
    import UIKit
    class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //基本警告框的使用
        self.baseAlertController()
        //警告框的动作列表样式
        self.actionSheetController()
    }
    func actionSheetController()  {
        let btn = UIButton(type:UIButtonType.system)
        btn.frame = CGRect(x:20,y:220,width:200,height:44)
        btn.setTitle("Question", for: UIControlState())
        btn.addTarget(self, action: #selector(ViewController.showActionSheet), for: UIControlEvents.touchUpInside)
        btn.backgroundColor = UIColor.lightGray
        self.view.addSubview(btn)
    }
    
    func showActionSheet()  {
        let alert = UIAlertController(title:"Infomation",message:"你最喜欢干什么?",preferredStyle:UIAlertControllerStyle.actionSheet)
                   
        let yes = UIAlertAction(title:"🐟",style:UIAlertActionStyle.default,handler:{(alerts:UIAlertAction) -> Void in
            //暂时理解为回调
            print("我喜欢吃鱼")
            
        })
            //         public convenience init(title: String?, style:     UIAlertActionStyle, handler: ((UIAlertAction) -> Swift.Void)? = nil)
        let no  = UIAlertAction(title:"hunting",style:UIAlertActionStyle.destructive,handler:{(alerts:UIAlertAction) -> Void in
            print("i like hunting")})
        
        let unKnown = UIAlertAction(title:"undo" ,style:UIAlertActionStyle.cancel,handler:{(alerts:UIAlertAction) -> Void in
            print("不知道什么操作")
        })
        
        
        alert.addAction(yes)
        alert.addAction(no)
        alert.addAction(unKnown)
        
        self.present(alert,animated: true,completion: nil)
    
    }
    func baseAlertController() {
        let btn = UIButton(type:UIButtonType.system)
        btn.frame = CGRect(x:20,y:120,width:200,height:44)
        btn.setTitle("Question", for: UIControlState())
        btn.addTarget(self, action: #selector(ViewController.alert), for: UIControlEvents.touchUpInside)
        btn.backgroundColor = UIColor.lightGray
        self.view.addSubview(btn)
    }
    
    func alert()  {
        let alert = UIAlertController(title:"Infomation",message:"Are you a student ?",preferredStyle:UIAlertControllerStyle.alert)
    
        let yes = UIAlertAction(title:"Yes",style:UIAlertActionStyle.default,handler:{(alerts:UIAlertAction) -> Void in
            //暂时理解为回调
            print("yes ,I'm a student")
            
        })
    
        let no  = UIAlertAction(title:"No",style:UIAlertActionStyle.default,handler:{(alerts:UIAlertAction) -> Void in
            print("No,I'm not a student")})
        let unKnown = UIAlertAction(title:"undo" ,style:UIAlertActionStyle.default,handler:{(alerts:UIAlertAction) -> Void in
             print("不知道什么操作")
        })
        alert.addAction(yes)
        alert.addAction(no)
        alert.addAction(unKnown)
        
        self.present(alert,animated: true,completion: nil)
    
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        }
    }
    

    相关文章

      网友评论

        本文标题:swift-UIAlertController

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