文丨清枫
在ios8之后UIAlertView和UIActionSheet变成了用UIAlertController表示首先在storyboard中创建一个按钮来测试
- (IBAction)Show:(id)sender {
NSString *title = NSLocalizedString(@"标题", nil);
NSString *message = NSLocalizedString(@"描述", nil);
NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
// Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"The \\\\\\\\\\\\\\\\"Okay/Cancel\\\\\\\\\\\\\\\\" alert's cancel action occured.");
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"The \\\\\\\\\\\\\\\\"Okay/Cancel\\\\\\\\\\\\\\\\" alert's other action occured.");
}];
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:otherAction]; [self presentViewController:alertController animated:YES completion:nil];
}
以上代码的效果如图

如果要加入输入框,要在Create the actions之前加上
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification
object:textField];
textField.secureTextEntry = YES;
//可以设置颜色
//textField.backgroundColor = [UIColor orangeColor];
}];
效果如图

如果在底部弹出,代码如下
NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
NSString *destructiveButtonTitle = NSLocalizedString(@"OK", nil); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
// Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"The \\\\\\\\\\\\\\\\"Okay/Cancel\\\\\\\\\\\\\\\\" alert action sheet's cancel action occured.");
}];
UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"The \\\\\\\\\\\\\\\\"Okay/Cancel\\\\\\\\\\\\\\\\" alert action sheet's destructive action occured.");
}];
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:destructiveAction];
[self presentViewController:alertController animated:YES completion:nil];}
效果如图

网友评论