標籤:
1.
Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert.
//UIAlertView和UIAlertViewDelegate(代理被給用block回調,更簡單)在iOS8及以後版本中被棄用,改用風格為UIAlertControllerStyleAlert的UIAlertController來替代。
2.
In apps that run in versions of iOS prior to iOS 8, use the UIAlertView class to display an alert message to the user. An alert view functions similar to but differs in appearance from an action sheet (an instance of UIActionSheet).
//iOS8以前版本中UIAlertView和UIActionSheet有著類似的功能,卻通過不同的類來產生。言外之意,iOS8以後版本,UIAlertView和UIActionSheet兩種alert頁面都將通過UIAlertController來產生。
3.
iOS 8以前版本 如何建立UIAlertView ?
OBJECTIVE-C
- (instancetype)initWithTitle:(NSString *)title
message:(NSString *)message
delegate:(id)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles,
, ...
4.
iOS 8及以後版本 如何建立UIAlertView ?
- UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
- message:@"This is an alert.” preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK”style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
- [alert addAction:defaultAction];
- [self presentViewController:alert animated:YES completion:nil];
5.
iOS 8以前版本 如何建立UIActionSheet ?
- (instancetype)initWithTitle:(NSString *)title
delegate:(id<UIActionSheetDelegate>)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles
, ...
6.
iOS 8及以後版本 如何建立UIActionSheet ?
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"蒼老師你好" message:@"聽說你的新片被下載了9999次" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.lblTarget.text = [NSString stringWithFormat:@"點擊AlertView確定按鈕後,產生隨機數"];
self.lblTarget.textColor = [UIColor redColor];
}]; //點擊按鈕後通過block回調執行此方法,故沒必要再使用代理了
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]; /*UIAlertActionStyleCancel 藍色字型,加粗;
UIAlertActionStyleDefault 字型藍色,不加粗;
UIAlertActionStyleDestructive字型紅色,不加粗;
*/
[alertVC addAction:action1];
[alertVC addAction:action2];
[self presentViewController:alertVC animated:YES completion:nil];
iOS 8及以後版本 如何建立UIAlertView?