昨天分享如何从中间弹出、由下而上弹出 UIAlertController
今天会介绍:
显示多个按钮,并设定点选按钮时做的事情
按了按钮後,执行{ action in }
里的动作
let alertController = UIAlertController(title: "alert title", message: "alert message", preferredStyle: .alert)
func merge(mergetitle: String, mergestyle: UIAlertAction.Style ,mergehandler: ((UIAlertAction) -> Void)?) {
alertController.addAction(UIAlertAction(title: mergetitle, style: mergestyle, handler: mergehandler))
}
merge(mergetitle: "Ok" , mergestyle: UIAlertAction.Style.default, mergehandler: { action in print("Ok")})
merge(mergetitle: "Cancel" , mergestyle: UIAlertAction.Style.cancel, mergehandler: { action in print("Cancel")})
merge(mergetitle: "Destructive" , mergestyle: UIAlertAction.Style.destructive, mergehandler: { action in print("Destructive")})
present(alertController, animated: true, completion: nil)
在 alert 里显示 TextField
按了"确认"的按钮後,执行print ( phone as Any, password as Any )
let alertController = UIAlertController(title: "登入", message: "输入电话和密码", preferredStyle: .alert)
// 参数 configurationHandler 传入的 closure 设定 text field 的样式
alertController.addTextField { textField in
textField.placeholder = "电话"
textField.keyboardType = UIKeyboardType.phonePad // 跳出电话键盘
}
alertController.addTextField { textField in
textField.placeholder = "密码"
textField.isSecureTextEntry = true // 密码隐藏
}
// 必须加上 capture list [unowned controller],避免 reference cycle 造成的记忆体问题
let alertOkAction = UIAlertAction(title: "确认", style: .default) { [unowned alertController] _ in
let phone = alertController.textFields?[0].text
let password = alertController.textFields?[1].text
print(phone as Any, password as Any)
}
alertController.addAction(alertOkAction)
let alertCancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
alertController.addAction(alertCancelAction)
present(alertController, animated: true, completion: nil)
Hint:
利用 UIAlertAction 生成视窗上的按钮
style :可控制文字的样式颜色
.default:预设( 蓝字 )
.destructive:红字
handler 传入的 closure :控制点选按钮要做的事情
利用 addAction() 加入按钮; 呼叫几个 addAction,即可加入几个按钮。
底部提示框不能加入输入框。
如果有加入Cancel按钮到底部提示框,它会永远在最底下一个。
这样就学会 UIAlertController 的使用啦!明天会有新的实作分享,敬请期待!
接续上一篇关於专案参加角色与pipeline的介绍,这一篇继续谈论每一区块需要的服务以及如何依照使用...
● 这章会示范如何透过自己的证券户做登入以及汇入凭证 登入(Login) 之前几章我们所使用 Shi...
前面在讲物件型别的时候只稍微谈到阵列,而其实阵列包含很多种 methods 可以运用,这篇要来练习阵...
Vim的思维、哲学与解决问题之道 [系列文目录] 每种工具都有它的设计理念,在接触Vim的前後,我们...
哈哈 低效率者当然是在说我啦 (抱歉 文章分类只有技术可选择 这篇算是个人碎念日记啦) 进修时期总是...