本次作业比较简单,只涉及到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.
}
}