一般在if判斷中加入
1.第一種基礎型

全都在xxx.m功能文件中編寫
UIAlertView(基礎版):
1 UIAlertView *WXinstall=[[UIAlertView alloc]initWithTitle:@"提示框" message:@"提示信息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];//一般在if判斷中加入 2 [WXinstall show];
1 //監聽點擊事件 代理方法 2 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 3 { 4 NSString *btnTitle = [alertView buttonTitleAtIndex:buttonIndex]; 5 if ([btnTitle isEqualToString:@"取消"]) { 6 NSLog(@"你點擊了取消"); 7 }else if ([btnTitle isEqualToString:@"確定"] ) { 8 NSLog(@"你點擊了確定"); 9 NSString *str = [NSString stringWithFormat: 10 @"https://itunes.apple.com/cn/app/wei-xin/id414478124?mt=8"];11 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
12 }//https在iTunes中找,這里的事件是前往手機端App store下載微信
13 }
UIAlertController(基礎版):
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示信息" preferredStyle:UIAlertControllerStyleAlert];//UIAlertControllerStyleAlert視圖在中央
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *str = [NSString stringWithFormat:
@"https://itunes.apple.com/cn/app/wei-xin/id414478124?mt=8"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}];//https在iTunes中找,這里的事件是前往手機端App store下載微信
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
2.第二種沒得選型。

UIAlertView(基礎版):
1 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"你確定要退出應用嗎?" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 2 [alert show];
3.第三種多選型。

//UIAlertControllerStyleAlert在中央屏幕。
//UIAlertControllerStyleActionSheet在屏幕底部。
UIAlertView(基礎版):iOS8都棄用這里就略。
UIAlertController(基礎版):
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet]; //UIAlertControllerStyleAlert在中央屏幕。 //UIAlertControllerStyleActionSheet在屏幕底部。 UIAlertAction *useCamera = [UIAlertAction actionWithTitle:@"使用相機拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"這里是要調用相機拍照功能"); }]; UIAlertAction *desAction = [UIAlertAction actionWithTitle:@"destructive" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"這里是要調用銷毀功能"); }]; UIAlertAction *usePhoto = [UIAlertAction actionWithTitle:@"使用相冊照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"這里是要調用相冊功能"); }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [alertController addAction:useCamera]; [alertController addAction:desAction]; [alertController addAction:usePhoto]; [alertController addAction:cancelAction]; [self presentViewController:alertController animated:YES completion:nil];
如果手機沒有安裝微信客戶端的情況下,不要提示用戶去安裝,因為可能遇到審核的人不小心把你的應用拒了。
UIAlertView和UIActionSheet在iOS8已經過期了,你仍然可以繼續使用。UIAlertController這個接口類是一個定義上的提升,它添加簡單,展示Alert和ActionSheet使用統一的API。因為UIAlertController使UIViewController的子類,他的API使用起來也會比較熟悉!
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"選擇睡前時間(分鍾)" message:nil preferredStyle:UIAlertControllerStyleAlert]; NSArray *timeArr = @[@"5",@"10",@"15",@"20",@"30",@"60"]; NSInteger timeCount = [timeArr count]; for (int i=0; i<timeCount; i++) { [alertVC addAction:[UIAlertAction actionWithTitle:timeArr[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [sender setTitle:action.title forState:UIControlStateNormal]; }]]; } [self presentViewController:alertVC animated:YES completion:nil];
