UIAlertController是iOS8出的,他把AlertView和sheet整合到一起了
他的自定义性比alertView要好的多。
不多说直接看代码吧
- (IBAction)AlertNew:(id)sender{
// UIAlertControllerStyleAlert
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示"
message:@"UIAlertController Demo"
preferredStyle:UIAlertControllerStyleAlert];
[alertVC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"登录";
//block里可以为控件添加相应的操作
//监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldValueChange:) name:UITextFieldTextDidChangeNotification object:textField];
}];
[alertVC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}];
okAction.enabled = NO;
//添加
[alertVC addAction:okAction];
//显示,现在是Controller,所以模态让他显示
[self
presentViewController:alertVC animated:YES
completion:^{
}];
}
- (IBAction)SheetNew:(id)sender
{
//枚举为 UIAlertControllerStyleActionSheet
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"信息" preferredStyle:UIAlertControllerStyleActionSheet];
//如果上拉菜单中有“取消(类型)”按钮的话,那么它永远都会出现在菜单的底部,不管添加的次序是如何
//Cancel类型
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
//Default类型
UIAlertAction *save = [UIAlertAction actionWithTitle:@"选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//设置代理人,需要签两份协议,一个是UINavigationControllerDelegate, 另一个是UIImagePickerControllerDelegate
picker.delegate = self;
//设置允许进行修改
picker.allowsEditing = YES;
//设置模态跳转
[picker setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:picker animated:YES completion:^{
}];
}];
//Destructive类型,为红色
UIAlertAction *delete = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
}];
//添加
[alertVC addAction:cancel];
[alertVC addAction:save];
[alertVC addAction:delete];
//显示
[self presentViewController:alertVC animated:YES completion:^{
}];
//枚举为 UIAlertControllerStyleActionSheet
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"信息" preferredStyle:UIAlertControllerStyleActionSheet];
//如果上拉菜单中有“取消(类型)”按钮的话,那么它永远都会出现在菜单的底部,不管添加的次序是如何
//Cancel类型
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
//Default类型
UIAlertAction *save = [UIAlertAction actionWithTitle:@"选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//设置代理人,需要签两份协议,一个是UINavigationControllerDelegate, 另一个是UIImagePickerControllerDelegate
picker.delegate = self;
//设置允许进行修改
picker.allowsEditing = YES;
//设置模态跳转
[picker setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:picker animated:YES completion:^{
}];
}];
//Destructive类型,为红色
UIAlertAction *delete = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
}];
//添加
[alertVC addAction:cancel];
[alertVC addAction:save];
[alertVC addAction:delete];
//显示
[self presentViewController:alertVC animated:YES completion:^{
}];
}