UIAlertController的使用

前几天在使用xcode8.2 的时候发现UIAlertView 和 UIActionSheet 已经划线被苹果抛弃了。至于之前的版本继续使用UIAlertView这两个控件会不会有问题,答案是不会的。只是苹果不会对其进行更新和维护了,就是说可能以后会有新功能,或者bug 苹果都不会对这两个控件进行更新了。

既然苹果开拓了这新控件,那我们就使用好了!个人觉得比以前的控件便捷多了,把之前的代理方法都集成到block中,方便易用。下面直接上代码:

/*

创建实例这个控制器有个preferreStyle属性你可以根据这个属性来确定是使用UIAlertView还是UIActionSheet

UIAlertControllerStyleActionSheet

UIAlertControllerStyleAlert

*/

UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:@"测试标题"message:@"提示信息"preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction*okAction = [UIAlertActionactionWithTitle:@"ok"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

NSLog(@".....点击确定后。。。");

}];

UIAlertAction*cancelAction2 = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

NSLog(@".....点击取消后。。。");

}];

UIAlertAction*cancelAction3 = [UIAlertActionactionWithTitle:@"其他"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

NSLog(@".....点击取消后。。。");

}];

[alertaddTextFieldWithConfigurationHandler:^(UITextField*_NonnulltextField) {

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(alertTextFieldDidChange:)name:UITextFieldTextDidChangeNotificationobject:textField]}];

[alertaddAction:okAction];

[alertaddAction:cancelAction2];

[alertaddAction:cancelAction3];

[selfpresentViewController:alertanimated:YEScompletion:nil];

}];

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(alertTextFieldDidChange:)name:UITextFieldTextDidChangeNotificationobject:textField];

}];

下面是运行的结果(三个action、两个action):

三个action的时候
两个action
UIAlertControllerStyleActionSheet
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容