Alert用于向用户提供重要信息。只有在Alert视图中选择了选项,我们才能进一步使用该应用程序。
- (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>来使您的类符合警报视图委托协议,如ViewController.h中所示。
#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; } } }
更新ViewController.m中的viewDidLoad,如下所示:
(void)viewDidLoad { [super viewDidLoad]; [self addAlertView]; }
运行应用程序时,将获得以下输出-
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)