ios7_如何?UIAlertView以及監聽點擊事件(預設樣式)
1、在viewController的.m檔案,遵守<UIAlertViewDelegate>代理協議 @interface moboViewController () <UIAlertViewDelegate> 2、在 - (void)viewDidLoad 方法中實現UIAlertView 複製代碼 1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 5 // 1、初始化 alertView 6 UIAlertView *altView = [[UIAlertView alloc]initWithTitle:[self altTitle] message:[self altMSG] delegate:self cancelButtonTitle:[self cancelBtnTitle] otherButtonTitles:[self otherBtnTitle], nil]; 7 // 2、顯示 alertView 8 [altView show]; 9 10 }複製代碼 3、繼續實現其它方法,返回各種標題以及監聽按鈕點擊事件的代理方法 複製代碼 1 //返回 標題 2 - (NSString *)altTitle{ 3 return @"下線通知"; 4 } 5 6 //返回 訊息體 7 - (NSString *)altMSG{ 8 return @"你的帳號在異地登入,密碼可能泄露,建議前往http://mobodemy.com進行修改。"; 9 }10 11 //返回 退出按鈕 標題12 - (NSString *) cancelBtnTitle {13 return @"退出";14 }15 16 //返回 重新登入 按鈕標題17 - (NSString *) otherBtnTitle {18 return @"重新登入";19 }20 21 //監聽點擊事件 代理方法22 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex23 {24 NSString *btnTitle = [alertView buttonTitleAtIndex:buttonIndex];25 if ([btnTitle isEqualToString:[self cancelBtnTitle]]) {26 NSLog(@"你點擊了退出");27 }28 else if ([btnTitle isEqualToString:[self otherBtnTitle]] ) {29 NSLog(@"你點擊了重新登入按鈕");30 }31 }