在最近學(xué)習(xí)的iOS開發(fā)中用到了一些警告提示框,看到的很多教程里用的還是UIAlertview,但是iOS9.0開始,xcode里已經(jīng)明確提示了不推薦使用,而是用UIAlertController來代替。
記錄一下我用到的幾種情況:
1.普通的警告提示框,顯示在屏幕正中心,有一個標(biāo)題,一段內(nèi)容,以及一個確定按鈕和一個取消按鈕:
NSString *title = @"這是標(biāo)題";
NSString *message = @"這是提示內(nèi)容";
NSString *cancelButtonTitle = NSLocalizedString(@"取消", nil);
NSString *otherButtonTitle = NSLocalizedString(@"確定", nil);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self startAgain];
}];
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[self presentViewController:alertController animated:YES completion:nil];
當(dāng)然也可以根據(jù)實際情況去掉一個按鈕。
2.從下方向上顯示的,多用于選擇相機(jī)還是相冊這類情況:
UIAlertController *_alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[_alertController addAction:[UIAlertAction actionWithTitle:@"拍攝" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self takePhoto];
}]];
[_alertController addAction:[UIAlertAction actionWithTitle:@"從相冊選擇" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self choosePhotoFromLibrary];
}]];
[_alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
_alertController = nil;
}]];
[self presentViewController:_alertController animated:YES completion:nil];
暫時我就用到了這兩種情況,當(dāng)然還有帶一個輸入框的,以及多個輸入框的,帶有警示按鈕的等等,這些都還沒用到,等用到了再記錄下來。