一、创建一个简单提醒,如下图
1,拖一个button控件到storyboard
2, 修改按钮标题
3,创建输出口
代码部分:
@IBAction func alertMe(sender: AnyObject) {
let alertController = UIAlertController(title: "提醒我", message: "记得就好了", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)
presentViewController(alertController, animated: true, completion: nil)
}
运行项目,测试!
二、创建多个按钮的提醒,如下图
1,拖一个button控件到storyboard
2,拖一个label控件到storyboard
3,创建输出口
代码部分:
@IBAction func alertButton(sender: AnyObject) {
let alertController = UIAlertController(title: "多个按钮的提醒", message: "选择一个吧", preferredStyle: .Alert)
let nowAction = UIAlertAction(title: "现在做些什么呢", style: .Default, handler: {(alertAction: UIAlertAction) in
self.userOutput.text = "现在已经选择了"
})
let laterAction = UIAlertAction(title: "稍后做些什么呢", style: .Default, handler: {(alertAction: UIAlertAction) in
self.userOutput.text = "稍后已经选择了"
})
let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler:nil)
alertController.addAction(nowAction)
alertController.addAction(laterAction)
alertController.addAction(cancelAction)
presentViewController(alertController, animated: true, completion: nil)
}
运行项目,测试!
点击其中任意按钮,标签会输入相应信息
三、在提醒框中输入信息,如下图
1,拖一个button控件到storyboard
2,拖一个label控件到storyboard
3,创建输出口
代码部分:@IBAction func alertInput(sender: AnyObject) {
let alertController = UIAlertController(title: "手机号码", message: "请输入你的手机号码", preferredStyle: .Alert)
alertController.addTextFieldWithConfigurationHandler({(UITextField: UITextField) in
UITextField.placeholder = "手机号码"
UITextField.keyboardType = UIKeyboardType.PhonePad
})
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: {(alertAction: UIAlertAction) in
let phone = alertController.textFields![0].text!
self.userOutput.text = "\(phone)"
})
alertController.addAction(defaultAction)
presentViewController(alertController, animated: true, completion: nil)
}
运行项目,测试!
点击按钮
输入一个号码
点击OK,标签会显示刚才输入的号码。