IOS開發(1)之UIAlertView

來源:互聯網
上載者:User

1.前言
之前簡單的學習了Objective-C的基礎文法,從今天起我們開始學習簡單的IOS視圖開發。

2.UIAlertView入門
2.1普通彈框
使用提示視圖的最好方法,當然是使用特定的初始化方法:

 

[plain]
- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
     
    //Title:這個字串會顯示在提示視圖的最上面的Title。 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" 
    //message:這是要給使用者看的實際訊息。 
    message:@"Message" 
    //delegate:我們可以傳遞委派物件(可選)給提示視圖。當檢視狀態變更時,委派物件會被通知。傳遞的參數對象必須實踐UIAlertViewDelegate協定. 
    delegate:nil 
    //cancelButtonTitle:選擇性參數。這個字串符會顯示在提示視圖的取消按鈕上。通常有取消按鈕的提示視圖都是要要求使用者做確認,使用者若不願意進行該動作,就會按下取消。這個按鈕的的標是可以自行設定的,不一定會顯示取消。 
    cancelButtonTitle:@"Cancel" 
    //otherButtonTitles:選擇性參數。若你希望提示視圖出現其他按鈕,只要傳遞標題參數。此參數需用逗號分隔,用 nil 做結尾。 
    otherButtonTitles:@"Ok", nil]; 
    [alertView show]; 

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
   
    //Title:這個字串會顯示在提示視圖的最上面的Title。
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
    //message:這是要給使用者看的實際訊息。
    message:@"Message"
    //delegate:我們可以傳遞委派物件(可選)給提示視圖。當檢視狀態變更時,委派物件會被通知。傳遞的參數對象必須實踐UIAlertViewDelegate協定.
    delegate:nil
    //cancelButtonTitle:選擇性參數。這個字串符會顯示在提示視圖的取消按鈕上。通常有取消按鈕的提示視圖都是要要求使用者做確認,使用者若不願意進行該動作,就會按下取消。這個按鈕的的標是可以自行設定的,不一定會顯示取消。
    cancelButtonTitle:@"Cancel"
    //otherButtonTitles:選擇性參數。若你希望提示視圖出現其他按鈕,只要傳遞標題參數。此參數需用逗號分隔,用 nil 做結尾。
    otherButtonTitles:@"Ok", nil];
    [alertView show];
}


運行結果:

 

 
 

 

2.2代理彈框
.h檔案:


[plain]
@interface ZYAlertYesOrNoViewController : UIViewController<UIAlertViewDelegate>//增加UIAlertViewDelegate代理 
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; 
@end 

@interface ZYAlertYesOrNoViewController : UIViewController<UIAlertViewDelegate>//增加UIAlertViewDelegate代理
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
@end.m檔案:


[plain]
- (void)viewDidAppear:(BOOL)animated{ 
    [super viewDidAppear:animated]; 
    //初始化UIAlertView 
    self.view.backgroundColor = [UIColor whiteColor]; 
    UIAlertView *alertView = [[UIAlertView alloc] 
                              initWithTitle:@"Rating" 
                              message:@"Can you please rate our app?" 
                              //為自身添加代理 
                              delegate:self 
                              cancelButtonTitle:[self noButtonTitle] 
                              otherButtonTitles:[self yesButtonTitle], nil]; 
    [alertView show]; 

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //初始化UIAlertView
    self.view.backgroundColor = [UIColor whiteColor];
    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Rating"
                              message:@"Can you please rate our app?"
                              //為自身添加代理
                              delegate:self
                              cancelButtonTitle:[self noButtonTitle]
                              otherButtonTitles:[self yesButtonTitle], nil];
    [alertView show];
}
[plain]

- (NSString *) yesButtonTitle{ return @"Yes"; 

- (NSString *) noButtonTitle{ return @"No"; 

//判斷使用者按下的是Yes還是No 
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
        NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex]; 
        if ([buttonTitle isEqualToString:[self yesButtonTitle]]) { 
           NSLog(@"User pressed the Yes button."); 
        }else if([buttonTitle isEqualToString:[self noButtonTitle]]){ 
            NSLog(@"User pressed the No button."); 
        } 

- (NSString *) yesButtonTitle{ return @"Yes";
}
- (NSString *) noButtonTitle{ return @"No";
}
//判斷使用者按下的是Yes還是No
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
        if ([buttonTitle isEqualToString:[self yesButtonTitle]]) {
           NSLog(@"User pressed the Yes button.");
        }else if([buttonTitle isEqualToString:[self noButtonTitle]]){
            NSLog(@"User pressed the No button.");
        }
}
當點擊Yes按鈕後
運行結果(控制台顯示):


2013-04-22 11:21:33.675 UIAlertViewTestDemo[1147:c07] User pressed the Yes button.

 


2.3帶輸入框的Alert


[plain]
//登陸彈出框:一個文本輸入框,一個密碼框 
UIAlertView *alertView = [[UIAlertView alloc] 
                              initWithTitle:@"Password" message:@"Please enter your credentials" delegate:self 
                              cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
//設定AlertView的樣式 
[alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; 
[alertView show]; 

//登陸彈出框:一個文本輸入框,一個密碼框
UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Password" message:@"Please enter your credentials" delegate:self
                              cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
//設定AlertView的樣式
[alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[alertView show];
運行結果:

 

 


UIAlertView樣式:


[plain]
type enum{ 
UIAlertViewStyleDefalut=0;//預設樣式 
UIAlertViewStyleSecureTextInput,//密碼框 
UIAlertViewStylePlainTextInput,//文本輸入框 
UIAlertViewStyleLoginAndPasswordInput //有登陸效果的提示框 
}UIAlertViewStyle 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.