(作业)ActionSheet、Alert

本次作业比较简单,只涉及到ActionSheet和Alert,它们两个用法及其相似,只有一些细微的差别,而且需要说明的点也很少。废话不多说,先上要求
这里写图片描述

同样我们为了方便添加按钮,使用导航栏控制器作为UIWindow的根视图控制器

self.window?.rootViewController = UINavigationController(rootViewController: ViewController())

接下来就是一些必要的准备了,我们先声明两个属性,一个UILabel类型的,用于保存用户输入的用户名和密码,一个UIView,用于改变背景颜色进行交互。

var v: UIView!
var label: UILabel!

然后就是向控制器的根视图添加子视图并且在导航栏上添加两个按钮

title = "Alert"
        self.view.backgroundColor = UIColor.white

        label = UILabel(frame: CGRect(x: 0, y: 300, width: self.view.bounds.width, height: 70))
        label.textColor = UIColor.black
        label.numberOfLines = 0
        label.textAlignment = .center
        self.view.addSubview(label)

        v = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        v.backgroundColor = UIColor.cyan
        self.view.addSubview(v)

        let rightBtn = UIBarButtonItem(title: "Alert", style: .plain, target: self, action: #selector(alertLogin))
        self.navigationItem.rightBarButtonItem = rightBtn

        let leftBtn = UIBarButtonItem(title: "Action", style: .plain, target: self, action: #selector(actionSheet))
        self.navigationItem.leftBarButtonItem = leftBtn

我们先实现Alert的要求,我们这里的交互使用弹出一个提示框,让用户输入用户名和密码,然后在界面上显示输入的用户名和密码。

@objc func alertLogin() {
        let alert = UIAlertController(title: "填写信息", message: nil, preferredStyle: .alert)

        alert.addTextField { (tf) in
            tf.placeholder = "用户名"
        }
        alert.addTextField { (tf) in
            tf.placeholder = "密码"
        }

        let OKBtn = UIAlertAction(title: "确定", style: .default) { _ in
            let username = alert.textFields![0].text ?? ""
            let password = alert.textFields![1].text ?? ""
            let string = "用户名: \(username)\n密码: \(password)"
            self.label.text = string
        }
        let cancelBtn = UIAlertAction(title: "取消", style: .cancel, handler: nil)

        alert.addAction(OKBtn)
        alert.addAction(cancelBtn)

        self.present(alert, animated: true, completion: nil)
    }

最后我们再实现ActionSheet的要求,我们这里使用其弹出几个选择颜色的按钮,用户点击一个按钮将视图的背景颜色更改为相应的颜色。

@objc func actionSheet() {
        let action = UIAlertController(title: "选择颜色", message: nil, preferredStyle: .actionSheet)

        let redBtn = UIAlertAction(title: "红色", style: .default) { (_) in
            self.v.backgroundColor = UIColor.red
        }
        let greenBtn = UIAlertAction(title: "绿色", style: .default) { (_) in
            self.v.backgroundColor = UIColor.green
        }
        let blueBtn = UIAlertAction(title: "蓝色", style: .default) { (_) in
            self.v.backgroundColor = UIColor.blue
        }
        let yellowBtn = UIAlertAction(title: "黄色", style: .default) { (_) in
            self.v.backgroundColor = UIColor.yellow
        }
        let blackBtn = UIAlertAction(title: "黑色", style: .default) { (_) in
            self.v.backgroundColor = UIColor.black
        }
        let grayBtn = UIAlertAction(title: "灰色", style: .default) { (_) in
            self.v.backgroundColor = UIColor.gray
        }
        let cancelBtn = UIAlertAction(title: "取消", style: .cancel, handler: nil)

        action.addAction(redBtn)
        action.addAction(greenBtn)
        action.addAction(blueBtn)
        action.addAction(yellowBtn)
        action.addAction(blackBtn)
        action.addAction(grayBtn)
        action.addAction(cancelBtn)

        self.present(action, animated: true, completion: nil)
    }

我们来看看运行的效果吧:
这里写图片描述

博主个人认为alert和actionSheet没有太大的差别,两个的区别在于弹出的位置不同,并且actionSheet中不能添加textField(虽然它可以调用addTextField()方法,但在运行过程中会崩溃)

所有代码如下:
ViewController.swift

class ViewController: UIViewController{

    var v: UIView!
    var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        title = "Alert"
        self.view.backgroundColor = UIColor.white

        label = UILabel(frame: CGRect(x: 0, y: 300, width: self.view.bounds.width, height: 70))
        label.textColor = UIColor.black
        label.numberOfLines = 0
        label.textAlignment = .center
        self.view.addSubview(label)

        v = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        v.backgroundColor = UIColor.cyan
        self.view.addSubview(v)

        let rightBtn = UIBarButtonItem(title: "Alert", style: .plain, target: self, action: #selector(alertLogin))
        self.navigationItem.rightBarButtonItem = rightBtn

        let leftBtn = UIBarButtonItem(title: "Action", style: .plain, target: self, action: #selector(actionSheet))
        self.navigationItem.leftBarButtonItem = leftBtn

    }

    @objc func alertLogin() {
        let alert = UIAlertController(title: "填写信息", message: nil, preferredStyle: .alert)

        alert.addTextField { (tf) in
            tf.placeholder = "用户名"
        }
        alert.addTextField { (tf) in
            tf.placeholder = "密码"
        }

        let OKBtn = UIAlertAction(title: "确定", style: .default) { _ in
            let username = alert.textFields![0].text ?? ""
            let password = alert.textFields![1].text ?? ""
            let string = "用户名: \(username)\n密码: \(password)"
            self.label.text = string
        }
        let cancelBtn = UIAlertAction(title: "取消", style: .cancel, handler: nil)

        alert.addAction(OKBtn)
        alert.addAction(cancelBtn)

        self.present(alert, animated: true, completion: nil)
    }

    @objc func actionSheet() {
        let action = UIAlertController(title: "选择颜色", message: nil, preferredStyle: .actionSheet)

        let redBtn = UIAlertAction(title: "红色", style: .default) { (_) in
            self.v.backgroundColor = UIColor.red
        }
        let greenBtn = UIAlertAction(title: "绿色", style: .default) { (_) in
            self.v.backgroundColor = UIColor.green
        }
        let blueBtn = UIAlertAction(title: "蓝色", style: .default) { (_) in
            self.v.backgroundColor = UIColor.blue
        }
        let yellowBtn = UIAlertAction(title: "黄色", style: .default) { (_) in
            self.v.backgroundColor = UIColor.yellow
        }
        let blackBtn = UIAlertAction(title: "黑色", style: .default) { (_) in
            self.v.backgroundColor = UIColor.black
        }
        let grayBtn = UIAlertAction(title: "灰色", style: .default) { (_) in
            self.v.backgroundColor = UIColor.gray
        }
        let cancelBtn = UIAlertAction(title: "取消", style: .cancel, handler: nil)

        action.addAction(redBtn)
        action.addAction(greenBtn)
        action.addAction(blueBtn)
        action.addAction(yellowBtn)
        action.addAction(blackBtn)
        action.addAction(grayBtn)
        action.addAction(cancelBtn)

        self.present(action, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值