UIAlertController用于替代UIActionSheet 和 UIAlertView。
替代UIActionSheet :
UIAlertController * Alert = [UIAlertController
alertControllerWithTitle:@"Are you sure?" //标题
message:nil //标题下的信息
preferredStyle:UIAlertControllerStyleActionSheet//这里选择ActionSheet风格
];
//新建一个动作
UIAlertAction *noWayAction = [UIAlertAction actionWithTitle:@"No way!" //标题
style:UIAlertActionStyleCancel //动作类型
handler:
^(UIAlertAction *action) { NSLog(@"cancel button pressed");}];
[Alert addAction:noWayAction];//把动作添加到控制器中
//新建一个动作
UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"Yes,i'm Sure!"
style:UIAlertActionStyleDestructive
handler:nil];
[Alert addAction:yesAction];
[self presentViewController:Alert animated:YES completion:nil];//以动画的方式显示
替代UIAlertView:
NSString *msg = nil;
if(_nameField.text.length >0)
msg = [NSStringstringWithFormat:@"You can breath easy, %@, everthing went OK.",_nameField.text];
else
msg = [NSStringstringWithFormat:@"You can breath easy. everything went OK."];
UIAlertController *Alert1 = [UIAlertControlleralertControllerWithTitle:@"Something was done"
message:msg
preferredStyle:UIAlertControllerStyleAlert//这里选择Alert风格
];
UIAlertAction *OKAction = [UIAlertActionactionWithTitle:@"Phew"style:UIAlertActionStyleDefaulthandler:nil];
[Alert1 addAction:OKAction];
[selfpresentViewController:Alert1 animated:YEScompletion:nil];
UIAlertControllerStyle:
typedef enumUIAlertControllerStyle: NSInteger {
UIAlertControllerStyleActionSheet =0,//actionSheet风格
UIAlertControllerStyleAlert//Alert风格
} UIAlertControllerStyle;
UIAlertActionStyle:
typedef enumUIAlertActionStyle: NSInteger {
UIAlertActionStyleDefault =0,//默认
UIAlertActionStyleCancel,//取消
UIAlertActionStyleDestructive//确认
} UIAlertActionStyle;