无法为此找到明确且内容丰富的解释。
答案 0 :(得分:63)
在搜索了一个主题后,我没有 找到一个清晰的解释,即使在它的类参考 UIAlertController Reference
没关系,但对我来说还不够清楚。
因此,在收集了一些和平之后,我决定自己做出解释 (希望它有所帮助)
所以这就是:
UIAlertView
已弃用,如指出:
UIAlertView in Swift UIAlertController
应该在iOS8 +中使用
所以首先创建一个我们需要实例化它,
构造函数(init)获取3个参数:2.1 title:String - >警告对话框顶部显示的粗体文字
2.2 message:String - >较小的文字(几乎可以解释为自己)
2.3 prefferedStyle:UIAlertControllerStyle
- >在大多数情况下,定义对话框样式:UIAlertControllerStyle.Alert
现在要向用户展示,我们可以使用showViewController
或presentViewController
并将警报作为参数传递
要向用户添加一些互动,我们可以使用:
4.1
UIAlertController.addAction
创建按钮
4.2
UIAlertController.addTextField
创建文本字段
编辑注释:下面的代码示例,针对swift 3语法进行了更新
示例1 :简单对话
@IBAction func alert1(sender: UIButton) {
//simple alert dialog
let alert=UIAlertController(title: "Alert 1", message: "One has won", preferredStyle: UIAlertControllerStyle.alert);
//show it
show(alert, sender: self);
}
示例2 :带有一个输入textField&的对话框两个按钮
@IBAction func alert2(sender: UIButton) {
//Dialog with one input textField & two buttons
let alert=UIAlertController(title: "Alert 2", message: "Two will win too", preferredStyle: UIAlertControllerStyle.alert);
//default input textField (no configuration...)
alert.addTextField(configurationHandler: nil);
//no event handler (just close dialog box)
alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.cancel, handler: nil));
//event handler with closure
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction) in
let fields = alert.textFields!;
print("Yes we can: "+fields[0].text!);
}));
present(alert, animated: true, completion: nil);
}
示例3 :一个自定义输入textField&一键
@IBAction func alert3(sender: UIButton) {
// one input & one button
let alert=UIAlertController(title: "Alert 3", message: "Three will set me free", preferredStyle: UIAlertControllerStyle.alert);
//configured input textField
var field:UITextField?;// operator ? because it's been initialized later
alert.addTextField(configurationHandler:{(input:UITextField)in
input.placeholder="I am displayed, when there is no value ;-)";
input.clearButtonMode=UITextFieldViewMode.whileEditing;
field=input;//assign to outside variable(for later reference)
});
//alert3 yesHandler -> defined in the same scope with alert, and passed as event handler later
func yesHandler(actionTarget: UIAlertAction){
print("YES -> !!");
//print text from 'field' which refer to relevant input now
print(field!.text!);//operator ! because it's Optional here
}
//event handler with predefined function
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: yesHandler));
present(alert, animated: true, completion: nil);
}
希望它有所帮助,祝你好运; - )
答案 1 :(得分:11)
UIAlertController的一个实例可以在屏幕上以模态方式呈现,就像使用presentViewController:animated:completion:方法的任何其他UIViewController一样。 UIAlertController实例作为ActionSheet或AlertView的区别是创建它时传递的样式参数。
不再授权
如果您使用过UIActionSheet或UIAlertView,那么您知道从中获取回调的方法是使用一个类(在大多数情况下是ViewController)来实现UIActionSheetDelegate或UIAlertViewDelegate协议。有一些开源项目用基于块的回调替换了这种委托模式,但官方API从未更新过。 UIAlertController不使用委托。相反,它有一个UIAlertAction项的集合,它们使用闭包(如果使用Objective-C则使用块)来处理用户输入。
对于操作表
@IBAction func showActionSheet(sender: AnyObject) {
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: "Swiftly Now! Choose an option!", preferredStyle: .ActionSheet)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Just dismiss the action sheet
}
actionSheetController.addAction(cancelAction)
//Create and add first option action
let takePictureAction: UIAlertAction = UIAlertAction(title: "Take Picture", style: .Default) { action -> Void in
//Code for launching the camera goes here
}
actionSheetController.addAction(takePictureAction)
//Create and add a second option action
let choosePictureAction: UIAlertAction = UIAlertAction(title: "Choose From Camera Roll", style: .Default) { action -> Void in
//Code for picking from camera roll goes here
}
actionSheetController.addAction(choosePictureAction)
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
对于带文本字段的AlertView
@IBAction func showAlert(sender: AnyObject) {
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Alert", message: "Swiftly Now! Choose an option!", preferredStyle: .Alert)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Do some stuff
}
actionSheetController.addAction(cancelAction)
//Create and an option action
let nextAction: UIAlertAction = UIAlertAction(title: "Next", style: .Default) { action -> Void in
//Do some other stuff
}
actionSheetController.addAction(nextAction)
//Add a text field
actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
//TextField configuration
textField.textColor = UIColor.blueColor()
}
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
答案 2 :(得分:1)
自原始响应以来,某些语法已更改。以下是一些示例代码,如果用户未登录iCloud,则会提醒用户。
CKContainer.default().accountStatus { (accountStatus, error) in
switch accountStatus {
case .available:
print("iCloud Available")
case .noAccount:
print("No iCloud account")
//simple alert dialog
let alert=UIAlertController(title: "Sign in to iCloud", message: "This application requires iClound. Sign in to your iCloud account to write records. On the Home screen, launch Settings, tap iCloud, and enter your Apple ID. Turn iCloud Drive on. If you don't have an iCloud account, tap Create a new Apple ID", preferredStyle: UIAlertControllerStyle.alert);
//no event handler (just close dialog box)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil));
//show it
self.present(alert, animated: true, completion: nil)
case .restricted:
print("iCloud restricted")
case .couldNotDetermine:
print("Unable to determine iCloud status")
}
}