我找不到一个清晰明了的解释。
我找不到一个清晰明了的解释。
在搜索一个主题一段时间后,我没有找到清晰的解释,即使在它的类参考UIAlertController 参考中也是如此。
虽然还可以,但对我来说不够清晰。
所以,在收集了一些信息后,我决定自己做出解释(希望能有所帮助)。
接下来就是解释:
UIAlertView
已经取消使用了:
Swift 中的 UIAlertViewUIAlertController
应该在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: 具有一个输入文本框和两个按钮的对话框
@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: 一个自定义输入文本框和一个按钮
@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);
}
希望这能有所帮助,祝你好运 ;-)
与许多其他编程语言不同,Swift 不要求您在代码中的每个语句后面写分号 (;),尽管如果您愿意,可以这样做。
我个人更喜欢跨语言的代码风格,因此您可以在我的示例中看到它。 - Nikita Kurtin使用presentViewController:animated:completion:方法,可以将UIAlertController实例模态地呈现在屏幕上,就像任何其他UIViewController一样。当创建它时,使UIAlertController实例在作为ActionSheet或AlertView工作不同的是您传递的样式参数。
无需再使用代理
如果你使用过UIActionSheet或UIAlertView,那么你应该知道获取回调的方法是让类(在大多数情况下是ViewController)实现UIActionSheetDelegate或UIAlertViewDelegate协议。有一些开源项目用块回调代替了这种委托模式,但官方API从未更新过。UIAlertController不使用代理。相反,它有一系列UIAlertAction项,使用闭包(或如果您使用Objective-C,则使用块)来处理用户输入。
对于Action Sheet
@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)
}
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")
}
}
UIAlertController
来创建警告框和操作表。我发现示例是理解事物如何工作的最简单方法。这里有一个警告框示例和一个操作表示例。 - Suragch