斯威夫特 2.0:
使用警报控制器。
操作表示例:
let mediaActionSheet: UIAlertController = UIAlertController(title: "Media Action Sheet", message: "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
}
mediaActionSheet.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
}
mediaActionSheet.addAction(takePictureAction)
//Create and add a second option action
let choosePictureAction: UIAlertAction = UIAlertAction(title: "Choose From Gallery", style: .Default) { action -> Void in
//Code for picking from gallery goes here
}
mediaActionSheet.addAction(choosePictureAction)
//Present the AlertController
self.presentViewController(mediaActionSheet, animated: true, completion: nil)
警报示例:
1)
let simpleAlert = UIAlertController(title: "Simple Alert", message: "It is just awesome", preferredStyle: UIAlertControllerStyle.Alert);
//show it
showViewController(simpleAlert, sender: self);
2) 使用 TextField 发出警报。
let inputTextFieldAlert:UIAlertController = UIAlertController(title: " Input TextField Alert ", message: " Enter on the below TextField ", preferredStyle: UIAlertControllerStyle.Alert);
//default input textField (no configuration...)
inputTextFieldAlert.addTextFieldWithConfigurationHandler(nil);
//no event handler (just close dialog box)
inputTextFieldAlert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel, handler: nil));
//event handler with closure
inputTextFieldAlert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: {(action:UIAlertAction) in
let fields = inputTextFieldAlert.textFields!;
print("Output: "+fields[0].text!);
}));
presentViewController(inputTextFieldAlert, animated: true, completion: nil);
3)
var alert = UIAlertController(title: "TextField Alert", message: "Enter on the below TextField", preferredStyle: UIAlertControllerStyle.Alert);
//configured input textField
var field:UITextField?;
alert.addTextFieldWithConfigurationHandler({(input:UITextField)in
input.placeholder="Empty Dtaa ;-)";
input.clearButtonMode=UITextFieldViewMode.WhileEditing;
field=input;
});
//YES Handler
func yesHandler(actionTarget: UIAlertAction){
print(field!.text!);
}
//event handler with predefined function
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: yesHandler));
presentViewController(alert, animated: true, completion: nil);