IOS开发 UIAlertController详解及实例代码
IOS开发 UIAlertController详解
在iOS 8.0后,苹果弃用了UIAlertView和UIActionSheet,转而使用UIAlertController把之前的UIAlertView和UIActionSheet整合在一起。新版的API变得简洁了不少几行代码就可实现之前一大片代码的功能
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
NSLog(@"你好你好");
}];
UIAlertAction* defaultAction2 = [UIAlertAction actionWithTitle:@"OK2" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
NSLog(@"你好你好");
}];
[alert addAction:defaultAction];
[alert addAction:defaultAction2];
[self presentViewController:alert animated:YES completion:nil];
初始化AlertView没有太大区别,主要区别就是添加事件。苹果公司新添加了UIAlertAction专门用来添加事件。一个Action对应一个事件,添加到alert上就可以使用。
切换为ActionSheet只需要修改preferredStyle为UIAlertControllerStyleActionSheet
也可以添加输入框代码如下
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"输入用户名";
}];
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
详解iOS App中UISwitch开关组件的基本创建及使用方法
UISwitch组件就是我们平时在iOS设置菜单中开到的那种左右滑动的开关按钮,当然我们在开发时可以进行更多的自定义,这里我们就来详解iOS App中UISwitch开关组件的基本创建及使用方法2016-05-05
iOS11 SectionHeader 胡乱移动且滑动时出现重复内容的解决方法
这篇文章主要介绍了iOS11 SectionHeader 胡乱移动且滑动时出现重复内容的解决方法,需要的朋友可以参考下2017-11-11


最新评论