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

Swift UIAlterViewController

发布时间:2020-12-14 07:05:22 所属栏目:百科 来源:网络整理
导读:import UIKitclass ViewController: UIViewController { override func viewDidLoad() { super .viewDidLoad () //定义按钮,显示最简单的Alter let button1 = UIButton(type: UIButtonType .System ) button1 .frame = CGRectMake( 30 , 70 ,self .view .fr
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //定义按钮,显示最简单的Alter
        let button1 = UIButton(type: UIButtonType.System)
        button1.frame = CGRectMake(30,70,self.view.frame.size.width-60,35)
        button1.setTitle("最简单的Alter",forState: UIControlState.Normal)
        button1.addTarget(self,action: "button1Action:",forControlEvents: UIControlEvents.TouchUpInside)
        self.view .addSubview(button1)

        //定义按钮,显示带文本框的Alter
        let button2 = UIButton(type: UIButtonType.System)
        button2.frame = CGRectMake(30,125,35)
        button2.setTitle("带文本框的Alter",forState: UIControlState.Normal)
        button2.addTarget(self,action: "button2Action:",forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(button2)

        //定义按钮,显示上拉菜单
        let button3 = UIButton(type: UIButtonType.System)
        button3.frame = CGRectMake(30,180,35)
        button3.setTitle("上拉菜单",forState: UIControlState.Normal)
        button3.addTarget(self,action: "button3Action:",forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(button3)

    }

    func button1Action(sender :UIButton) {

        let alterController = UIAlertController(title: "最简单的Alter",message: nil,preferredStyle: UIAlertControllerStyle.Alert)
        let action1 = UIAlertAction(title: "星期一",style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in

        }
        let action2 = UIAlertAction(title: "星期二",style: UIAlertActionStyle.Destructive) { (UIAlertAction) -> Void in

        }
        let action3 = UIAlertAction(title: "星期三",style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in

        }
        let action4 = UIAlertAction(title: "取消",style: UIAlertActionStyle.Cancel) { (UIAlertAction) -> Void in

        }

        alterController.addAction(action1)
        alterController.addAction(action2)
        alterController.addAction(action3)
        alterController.addAction(action4)

        self .presentViewController(alterController,animated: true,completion: nil)
    }

    func button2Action(sender :UIButton) {

        let alterController = UIAlertController(title: "带文本框的Alter",preferredStyle: UIAlertControllerStyle.Alert)
        alterController.addTextFieldWithConfigurationHandler { (textField: UITextField) -> Void in
            textField.placeholder = "请输入账号"
        }
        alterController.addTextFieldWithConfigurationHandler { (textField: UITextField) -> Void in
            textField.placeholder = "请输入密码"
            textField.secureTextEntry = true
        }
        let action = UIAlertAction(title: "登录",style: UIAlertActionStyle.Cancel) { (UIAlertAction) -> Void in

        }

        alterController.addAction(action)

        self.presentViewController(alterController,animated: true) { () -> Void in

        }
    }

    func button3Action(sender: UIButton) {
        let alterController = UIAlertController(title: "上拉菜单",preferredStyle: UIAlertControllerStyle.ActionSheet)
        let action1 = UIAlertAction(title: "星期一",completion: nil)

    }

}

DEMO下载

(编辑:李大同)

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

    推荐文章
      热点阅读