#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
//************************只是为了创建一个点击事件,用来触发能用UIAlertController处理的事件*****************
//创建一个分段控件
UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"放弃",@"菜单"]];
segment.tintColor = [UIColor orangeColor];
segment.frame = CGRectMake(60, 100, 200, 40);
[segment addTarget:self action:@selector(handle:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segment];
[segment release];
}
//*******************处理点击事件 (用 UIAlertControl (ios 8 之后才能用😊😊 ) 处理)-------所讲重点;
/**
* 下面的是我所分享的这个空间,别看代码长,其实 case0 和 case1 是 里面几乎一样的代码,一个是为了处理警告框(UIAertView),另一个是处理操作表(UIActionSheet).其实 只是对UIAlertControl的初始化换了个类型而已,大家不要怕代码长;
都是重复的,功能挺强大的;大家认真看看😊😊😊😊
*
*(重点是 里面的Block 块,这里我只是打了个NSLog,大家下去自己多总结😄😄😄😄)
*/
- (void)handle:(UISegmentedControl *)sender{
switch (sender.selectedSegmentIndex) {
// 下面实现 用 UIAlertController处理 UIAlertView的功能:
case 0:{
//1 .创建对象
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要退出么?" preferredStyle: UIAlertControllerStyleAlert];
//2. 创建 触发项
//创建触发项时注意它的类型(Style) 是三个枚举值,有不同的作用!!
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^void(UIAlertAction *action){NSLog(@"什么都不操作😄😄");}];
//创建 第二个触发项 (还可以继续创建,这里只用两个)
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDestructive handler:^void(UIAlertAction *action){NSLog(@"您执行了您的操作😢");
}];
//3.添加触发项 (把刚才创建的触发项添加到这个UIAlertController的对象(alertVC)上)😄😄
[alertVC addAction:cancelAction];
[alertVC addAction:okAction];
//4.添加 输入框 实现用户账户的输入
[alertVC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"用户名";
}];
////添加 输入框 实现用户密码的输入
[alertVC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}];
//5.模态 alertVC 控制器 到模态窗口进行显示😄😄😄😄
[self presentViewController:alertVC animated:YES completion:nil];
}
break;
// 下面实现 用 UIAlertController处理 UIActionSheet的功能
case 1:
{
//1 .创建对象
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要退出么?" preferredStyle: UIAlertControllerStyleActionSheet];
//2. 创建 触发项
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^void(UIAlertAction *action){NSLog(@"什么都不操作😄😄");}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDestructive handler:^void(UIAlertAction *action){NSLog(@"您执行了您的操作😢");
}];
//3.添加触发项
[alertVC addAction:cancelAction];
[alertVC addAction:okAction];
//4.模态 alertVC 控制器 到模态窗口进行显示😄😄😄😄
[self presentViewController:alertVC animated:YES completion:nil];
// 注意: // 这个只有 四 步 哦! 比UiAlertView少了个 添加 输入框的功能,因为貌似 操作表没有添加输入框的功能!😄😄😄😄!
}
break;
default:
break;
}
}
//到这里 UIAlertController 的一个初始化方法 和 一个添加操作项的方法 以及一个为 警告框 添加文本框的方法都在上面的代码里了!这个控制器内容虽然不多,但可以实现的功能是很强大的,因为里面涉及了Block 块!所以 还是请大家自己去好好看看为好!这里就不多说了!😄😄
// 另外 UIAlertController 里面还有五个小的属性,其中三个是只读的,大家可以去头文件 或者 API里查看,这里就不介绍了! 大家再见啊!作👀👀👀👀👀
@end