iOS開發之UIAlertView與UIAlertController的詳盡用法說明


本文將從四個方面對IOS開發中UIAlertView與UIAlertController的用法進行講解:


一、UIAlertView與UIAlertController是什么東東?

二、我們為什么要用UIAlertView或UIAlertController?

三、如何使用UIAlertView和UIAlertController?

四、閱讀提醒。


一、UIAlertView與UIAlertController是什么東東?

 一句話,在所有移動應用中,出現的提示窗一般都由UIAlertView或UIAlertController來編寫的,兩者功能相同。

 


二、我們為什么要用UIAlertView或UIAlertController? 

  好的人機交互,可以提高用戶體驗,操作系統的人機交互功能是決定計算機系統“友善性”的一個重要因素。人機交互功能主要靠可輸入輸出的外部設備和相應的軟件來完成。在傳統時期,可供人機交互使用的設備主要有鍵盤顯示、鼠標、各種模式識別設備等。與這些設備相應的軟件就是操作系統提供人機交互功能的部分。早期的人機交互設施主要是鍵盤和顯示器。操作員通過鍵盤打入命令,操作系統接到命令后立即執行並將結果通過顯示器顯示。打入的命令可以有不同方式,但每一條命令的解釋是清楚的,唯一的。如今,在移動端我們所進行的所有操作均是在可觸摸屏幕上進行的,為了更好的進行人機交互,工程師便把操作信息或提示信息顯示到屏幕上,用戶根據自身判斷來決定所需點擊的按鈕。一句話,就是利用人機交互更好的提升用戶的使用體驗和產品設計的質量。

 


三、如何使用UIAlertView和UIAlertController?

 1、UIAlertView的使用方法:

    // Newly initialized alert view.
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Edit" message:@"Please Modify the Info" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Sure", @"Other", nil];
    
    // 為下面修改數據用
    // This property is inherited from the UIView, You can use this property to distinguish when a AlertView has multiple view
    alertView.tag = indexPath.row;
    
    // Adds a button to the receiver with the given title.
    [alertView addButtonWithTitle:@"addBtn"];
    
    /**
     UIAlertViewStyle = 以下4種
     UIAlertViewStyleDefault,
     UIAlertViewStyleSecureTextInput,       //密碼輸入框風格
     UIAlertViewStylePlainTextInput,        //普通輸入框風格
     UIAlertViewStyleLoginAndPasswordInput  //賬號密碼框風格
     */
    // An alert that allows the user to enter text. Available in iOS 5.0 and later.
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    
    // Returns the text field at the given index
    UITextField *textField = [alertView textFieldAtIndex:0];
    textField.text = model.title;
    
    // The number of buttons on the alert view. (read-only)
    NSLog(@"The total number of button is : %ld", alertView.numberOfButtons);
    
    // Returns the title of the button at the given index.
    NSLog(@"The button title at the specified index : %@", [alertView buttonTitleAtIndex:1]);
    
    // The index number of the cancel button.
    NSLog(@"Index for the cancel button is : %ld",alertView.cancelButtonIndex);
    
    // -1 if no otherButtonTitles or initWithTitle:... not used
    NSLog(@"The index of the first other button is (read-only) : %ld",alertView.firstOtherButtonIndex);
    
    // show UIAlertView
    [alertView show];
UIAlertView的一些基本屬性

 

 

 

/**
 //根據被點擊按鈕的索引處理點擊事件
  Called when a button is clicked. The view will be automatically dismissed after this call returns
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

//AlertView即將顯示時
-(void)willPresentAlertView:(UIAlertView *)alertView;

// AlertView已經顯示時的事件
-(void)didPresentAlertView:(UIAlertView *)alertView;

// ALertView即將消失時的事件
// This method is invoked before the animation begins and the view is hidden.
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex;

// AlertView已經消失時執行的事件
// This method is invoked after the animation ends and the view is hidden.
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;

// AlertView的取消按鈕的事件
// If not defined in the delegate, we simulate a click in the cancel button
-(void)alertViewCancel:(UIAlertView *)alertView;

 

 2、UIAlertController的使用方法:

  *  使用UIAlertController共需要三步

     *  1.實例化alert:alertControllerWithTitle

     *  2.實例化按鈕:actionWithTitle

     *  3.顯示alertController:presentViewController

// 1.實例化alert:alertControllerWithTitle
UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"編輯" message:@"請修改菜單名稱:" preferredStyle:UIAlertControllerStyleAlert];

// 2.實例化按鈕:actionWithTitle
// 為防止block與控制器間循環引用,我們這里需用__weak來預防
__weak typeof(alert) wAlert = alert;
    [alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        // 點擊確定按鈕的時候, 會調用這個block
        NSLog(@"%@",[wAlert.textFields.firstObject text]);
        
    }]];

    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];

    // 添加文本框(只能添加到UIAlertControllerStyleAlert的樣式,如果是preferredStyle:UIAlertControllerStyleActionSheet則會崩潰)
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        
        textField.text = model.title;
        
        //監聽文字改變的方法
        [textField addTarget:self action:@selector(textFieldsValueDidChange:) forControlEvents:UIControlEventEditingChanged];
    }];

    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        
        textField.secureTextEntry = YES;  // 密文形式顯示
        textField.text = model.price;
    }];

// 3.顯示alertController:presentViewController
[self presentViewController:alert animated:YES completion:nil];
UIAlertController的基本使用

 

 


四、閱讀提醒

  在Xcode的iOS8 SDK中,UIAlertView和UIAlertController都被UIAlertController取代。官方庫解釋:"UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead."、"UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead."。說明了在iOS8+開發,UIALertView和UIActionSheet已經過時了,UIAlertController以一種模塊化替換的方式來代替這兩這兩個控件的功能和作用。


免責聲明!

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



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