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 = @"輸入用戶名";
}];
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!