IOS警告對話框的使用
警告對話框用來給用戶提供重要資訊。
僅在警告對話框視圖中選擇選項後,才能著手進一步使用應用程式。
重要的屬性
- alertViewStyle
- cancelButtonIndex
- delegate
- message
- numberOfButtons
- title
重要的方法
- (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,如下所示
讓類符合警告對話框視圖的委託協議,如下所示,在ViewController.h中添加<UIAlertViewDelegate>
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIAlertViewDelegate>{ } @end
添加自定義方法 addAlertView
-(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]; }
輸出
現在當我們運行該應用程式我們會看到下麵的輸出: