警告对话框用来给用户提供重要信息。
仅在警告对话框视图中选择选项后,才能着手进一步使用应用程序。
- (NSInteger)addButtonWithTitle:(NSString *)title
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex
- (void)dismissWithClickedButtonIndex: (NSInteger)buttonIndex animated:(BOOL)animated
- (id)initWithTitle:(NSString *)title message: (NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString*)otherButtonTitles, ...
- (void)show
让类符合警告对话框视图的委托协议,如下所示,在ViewController.h中添加<UIAlertViewDelegate>
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIAlertViewDelegate>{ } @end
-(void)addAlertView{ UIAlertView *alertView = [[UIAlertView alloc]initWithTitle: @"Title" message:@"This is a test alert" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; [alertView show]; }
#pragma mark - Alert view delegate - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex{ switch (buttonIndex) { case 0: NSLog(@"Cancel button clicked"); break; case 1: NSLog(@"OK button clicked"); break; default: break; } }
(void)viewDidLoad { [super viewDidLoad]; [self addAlertView]; }
现在当我们运行该应用程序我们会看到下面的输出: