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)buttonIndex
23 {
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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM