標籤:
1. iOS7及iOS7之前警告類控制項有UIAlertView和UIActionSheet1.1 UIAlertView的使用
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"這是一個UIAlertView" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"關閉", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
/** alertViewStyle的枚舉值如下
UIAlertViewStyleDefault = 0,
UIAlertViewStyleSecureTextInput,
UIAlertViewStylePlainTextInput,
UIAlertViewStyleLoginAndPasswordInput
*/
[alert show];
// UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
1.2 UIActionSheet的使用
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"這是一個UIActionSheet" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"確定"
otherButtonTitles:@"關閉", nil];
[sheet showInView:self.view];
// UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
2. iOS8開始,用UIAlertController替代UIAlertView+UIActionSheet2.1 UIAlertController的使用 (Style: UIAlertControllerStyleAlert)
// 初始化UIAlertController
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"iOS新控制項UIAlertController" preferredStyle:UIAlertControllerStyleAlert];
// 添加按鈕,注意如果Block的循環參考
__weak typeof(alert) weakAlert = alert;
[alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
// block內部寫點擊按鈕之後做的事情
NSLog(@"點擊了確定按鈕--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);
}]];
// 添加文字框
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.textColor = [UIColor blueColor];
textField.text = @"請輸入使用者名稱";
[textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];
// 也可以用通知監聽TextField的變化
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(usernameDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.secureTextEntry = YES;
textField.text = @"123456";
}];
// 彈出UIAlertController
[self presentViewController:alert animated:YES completion:nil];
// textField EditingChanged時,回調的方法
- (void)usernameDidChange:(UITextField *)username
{
NSLog(@"%@", username.text);
}
2.2 UIAlertController的使用 (Style: UIAlertControllerStyleActionSheet)
// 在2.1代碼上,修改 初始化UIAlertController 的代碼
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"iOS新控制項UIAlertController" preferredStyle:
UIAlertControllerStyleActionSheet];
運行程式後報錯,因為text field只能用在Style:UIAlertControllerStyleAlert 的UIAlertController上,把添加textFiled的代碼登出掉,再運行程式就正常了
reason: ‘Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert‘
2.3通過上面的介紹,可以看出蘋果有意把UIAlertView,UIActionSheet統一在一起,實際上,蘋果還統一了iphone和ipad的UIAlertController的處理方式
2.3.1 建立項目時,Devices選擇Universal,語言選OC(錯誤)
2.3.2 在Main.storyboard中拖導航控制器,讓mainViewController成為導航控制器的rootViewController
2.3.3 程式碼範例
// 初始化UIAlertController
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"iOS新控制項UIAlertController" preferredStyle:
UIAlertControllerStyleAlert];
// 設定popover指向的item
alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;
// 添加按鈕
[alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"點擊了確定按鈕");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"點擊了取消按鈕");
}]];
// 彈出UIAlertController
[self presentViewController:alert animated:YES completion:nil];
注:左圖是在iphone上運行效果,右圖是在ipad上的運行效果,Style為UIAlertControllerStyleAlert時,顯示樣式一致
2.3.4 把2.3.3的初始化代碼改成Style:UIAlertControllerStyleActionSheet
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"iOS新控制項UIAlertController" preferredStyle:UIAlertControllerStyleActionSheet];
注:左圖是iphone,右圖是ipad,用一份代碼實現了在iphone和ipad上不同的顯示樣式
iOS iOS8新特性--UIAlertController