您的位置:首页 > 其它

文章标题

2015-10-21 09:03 489 查看
自从iOS8 苹果推出了UIAlertController用来代替UIAlertView和UIActionSheet。也许大部分开发者并没有注意到。那时候UIAlertView和UIActionView都还可以使用。但是自从iOS9后,那两个就不废除了。现在统一用UIAlertController代替。
UIAlertView和UIActionSheet大家都很熟悉了。他的点击响应事件是通过协议代理来完成的。但是我们的UIAlertController的响应时间是闭包实现的,而不是委托代理实现的。下面简单的展示一下实现的代码。
1、实现UIAlertView的代码。
UIAlertController *alertControl = [UIAlertControlleralertControllerWithTitle:@"Alert"message:@"Alert text goes here"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *noAction = [UIAlertActionactionWithTitle:@"No"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {
NSLog(@"Tap No Button");
}];
UIAlertAction *yesAction = [UIAlertActionactionWithTitle:@"Yes"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {
NSLog(@"Tap Yes Button");
}];
UIAlertAction *midleAction = [UIAlertActionactionWithTitle:@"Midle"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {
NSLog(@"Tap Yes Midle");
}];
[alertControl addAction:noAction];
[alertControl addAction:yesAction];
[alertControl addAction:midleAction];
[selfpresentViewController:alertControlanimated:truecompletion:nil];
2、实现UIActionSheet的代码。
UIAlertController *actionSheetControl = [[UIAlertControlleralloc]init];
UIAlertAction *cancalAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *_Nonnull action) {
NSLog(@"Tap取消 Button");
}];
UIAlertAction *destructiveAction = [UIAlertActionactionWithTitle:@"破坏性按钮"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {
NSLog(@"Tap破坏性按钮 Button");
}];
UIAlertAction *otherAction = [UIAlertActionactionWithTitle:@"其他"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {
NSLog(@"Tap other Button");
}];
[actionSheetControladdAction:cancalAction];
[actionSheetControladdAction:destructiveAction];
[actionSheetControladdAction:otherAction];
[selfpresentViewController:actionSheetControlanimated:YEScompletion:nil];
3、如果你的一个项目需要频繁的使用到警告框,每次都这么写有点繁琐,我们可以对其进行封装,你可以看到现在苹果是大力推荐使用block。所以我的封装也是使用block的。首先我们要创建一个类。同时倒入UIKit,否则不行的。详细的代码如下这是一个默认有两个按钮的警告框。我们需要传一个字典。字典中必须有title,message,oneAction,twoAction。


+(void)creatAlertView:(NSDictionary )contentDictory YesClick:(void (^)(void))yesClick NoClick:(void (^)(void))noClick alertView:(void (^) (UIAlertController alertView))alertView{

UIAlertController *alertControl = [UIAlertControlleralertControllerWithTitle:[contentDictoryobjectForKey:@”title”]message:[contentDictory objectForKey:@”message”]preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *noAction = [UIAlertActionactionWithTitle:[contentDictory objectForKey:@”oneAction”]style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {

yesClick();

}];

UIAlertAction *yesAction = [UIAlertActionactionWithTitle:[contentDictory objectForKey:@”twoAction”]style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {

noClick();

}];

[alertControl addAction:noAction];

[alertControl addAction:yesAction];

alertView(alertControl);

}

4、调用的方法是首先导入你创建的那个类。然后调用方法。具体的代码如下。

NSDictionary *contentDictory =@{@”title”:@”温馨提示”,@”message”:@”话费余额不足”,@”oneAction”:@”知道了”,@”twoAction”:@”去充话费”};

[AlertViewcreatAlertView:contentDictory YesClick:^{

NSLog(@”Yes”);

} NoClick:^{

NSLog(@”No”);

} alertView:^(UIAlertController *alertView) {

[selfpresentViewController:alertView animated:truecompletion:nil];

}];

5、当然UIAlertController也有别的新特性。其中最大的一个特性是文本的输入不受现在。并且可以自定义,不像以前的UIAlertView只可以输入两个文本框,并且只有一种样式。现在你只需要加一句代码就可以创建一个输入的文本框。代码是:

[alertControl addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {

textField.placeholder =@”话费余额”;

textField.text =@”账号:”;

NSLog(@”%@”,textField.text);

}];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: