Objective-C ,ios,iphone開發基礎:UIAlertView使用詳解


UIAlertView使用詳解

Ios中為我們提供了一個用來彈出提示框的類 UIAlertView,他類似於javascript中的alert c#中的MessageBox();

UIAlertView 繼承自 UIView @interface UIAlertView : UIView )

一、簡單的初始化一個UIAlertView 對象。

 UIAlertView* alert = [[UIAlertView alloc] init];

激活 alert ,讓它顯示。

 [alert show];

結果將如下:

 

 

這樣雖然出現了一個提示框,但是太不過友好,讓人根本無法使用。

二,帶有button的提示框。

UIAlertView 里面包含了另外一種用來初始化的方法。

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;

 

帶有一個button

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"簡單的提示框" message:@"simple alert" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];

[alert show];

 

 

帶有多個button

 UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"簡單的提示框" message:@"simple alert" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"Button1",@"Button2",@"Button3" ,nil];

[alert show];

 

要想處理多個button點擊之后做出不同響應,那么必須讓當前控制器類遵循 UIAlertViewDelegate 協議。

UIAlertViewDelegate 里面包含了一個方法(- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;),用來觸發在點擊button之后的操作,判斷是哪一個button 有兩種方式,一種是更具button 的索引,

另外一種是buttontitle

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    NSString* buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];

    if([buttonTitle isEqualToString:@"Button1"]){

        userOutput.text = buttonTitle;

    }if([buttonTitle isEqualToString:@"Button2"]){

        userOutput.text = buttonTitle;

    }if([buttonTitle isEqualToString:@"Button3"]){

        userOutput.text = buttonTitle;

    }if([buttonTitle isEqualToString:@"ok"]){

        userOutput.text = buttonTitle;

    }

}

 

 

 

三 、給提示框添加輸入框,最經典的案例,appstore 下載的時候輸入密碼、

   首先,初始化UITextField

   userInput = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 70.0, 260.0, 25.0)];

    [userInput setBackgroundColor:[UIColor whiteColor ]];

    將userInput  添加在 alert上,

 

    [alert addSubview:userInput];

 

 

- (IBAction)btnWithTextField:(id)sender {

    

    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Please Enter Your Email Address" message:@"simple alert" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];

    userInput = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 70.0, 260.0, 25.0)];

    [userInput setBackgroundColor:[UIColor whiteColor ]];

    [alert addSubview:userInput];

    [alert show];

    [alert release];

    [userInput release];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    NSMutableString* buttonTitle = [NSMutableString stringWithString:[alertView buttonTitleAtIndex:buttonIndex]];

    if([buttonTitle isEqualToString:@"Button1"]){

        userOutput.text = buttonTitle;

    }if([buttonTitle isEqualToString:@"Button2"]){

        userOutput.text = buttonTitle;

    }if([buttonTitle isEqualToString:@"Button3"]){

        userOutput.text = buttonTitle;

    }if([buttonTitle isEqualToString:@"ok"]){

        [buttonTitle appendString:userInput.text];

        userOutput.text = buttonTitle;

        

    }

}

 


免責聲明!

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



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