iOS 教程
警告對話框用來給用戶提供重要信息。
僅在警告對話框視圖中選擇選項后,才能著手進(jìn)一步使用應(yīng)用程序。
- (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
讓類符合警告對話框視圖的委托協(xié)議,如下所示,在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]; }
現(xiàn)在當(dāng)我們運行該應(yīng)用程序我們會看到下面的輸出: