iOS开发中的弹窗、快捷操作与本地通知
1. 弹窗与操作表
在iOS开发里,弹窗(Alert)和操作表(Action Sheet)是常用的交互组件。
1.1 弹窗文本输入处理
当在弹窗中添加文本输入框时,可按如下代码设置:
alert.addTextField { tf in
tf.keyboardType = .numberPad
tf.addTarget(self,
action: #selector(self.textChanged), for: .editingChanged)
}
上述代码为文本框设置了数字键盘类型,并添加了文本编辑改变的监听。当用户编辑文本时, textChanged
方法会被调用。为了在文本框有内容时启用弹窗的“确定”按钮,可通过响应链从文本框找到弹窗控制器:
@objc func textChanged(_ sender: Any) {
let tf = sender as! UITextField
var resp : UIResponder! = tf
while !(resp is UIAlertController) { resp = resp.next }
let alert = resp as! UIAlertController
alert.actions[1].isEnabled = (tf.text != "")
}