- iOS 8之后蘋果推薦我們使用的系統(tǒng)自帶的提示框是UIAlertController利虫。UIAlertController有兩種類型:
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);
它的出現(xiàn)是為了替帶之前的UIAlertView和UIActionSheet兩個(gè)類舶吗。 -
下面介紹一下UIAlertController,順便有示例花枫。
- 1.登錄型的提示框
- (IBAction)loginAlert:(UIButton *)sender {
//獲取用戶當(dāng)前手機(jī)的系統(tǒng)版本
[[UIDevice currentDevice] systemVersion];
//當(dāng)前的系統(tǒng)版本大于等于8.0
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"登錄" message:nil preferredStyle:UIAlertControllerStyleAlert];
//1.初始化action
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//找到輸入框?qū)ο?br> UITextField *usernameTF = alertController.textFields[0];
UITextField *passwordTF = alertController.textFields[1];
//取出用戶名和密碼
NSString *username = usernameTF.text;
NSString *password = passwordTF.text;
NSLog(@"username:%@\npassword:%@",username,password);
}];
//2.添加動(dòng)作
[alertController addAction:action1];
[alertController addAction:action2];
//添加輸入框
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"請輸入用戶名";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"請輸入密碼";
textField.secureTextEntry = YES;
}];
//顯示
[self presentViewController:alertController animated:YES completion:nil];
}
}
-
2.UIAlertControllerStyleAlert和UIAlertControllerStyleActionSheet類型的,這里的示例是傳入一個(gè)類型,然后根據(jù)類型展示不同風(fēng)格山憨。
- (void)btnTouchWithStyle:(UIAlertControllerStyle)style{
/*
preferredStyle:
1.UIAlertControllerStyleActionSheet
2.UIAlertControllerStyleAlert
*/
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
//iOS8以后才有的,替換了UIAlertView和UIActionSheet兩個(gè)類
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"分享" message:@"選擇分享的平臺(tái)" preferredStyle:style];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"取消按鈕點(diǎn)擊");
}];
UIAlertAction *qqAction = [UIAlertAction actionWithTitle:@"分享到QQ" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"分享到QQ");
}];
UIAlertAction *weixinAction = [UIAlertAction actionWithTitle:@"分享到微信" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"分享到微信");
}];//添加動(dòng)作 [alertController addAction:cancelAction]; [alertController addAction:qqAction]; [alertController addAction:weixinAction]; //顯示 [self presentViewController:alertController animated:YES completion:nil]; }else{ NSLog(@"當(dāng)前的系統(tǒng)版本是小于8.0的,使用UIAlertView"); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享" message:@"選擇分享的平臺(tái)" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil]; [alertView show]; } NSLog(@"------當(dāng)前的系統(tǒng)版本: %f---",[[[UIDevice currentDevice] systemVersion] floatValue]); }