IOS開發之UIAlertView與UIAlertController的詳盡用法說明,iosuialertview用法
本文將從四個方面對IOS開發中UIAlertView與UIAlertController的用法進行講解:
一、UIAlertView與UIAlertController是什麼東東?二、我們為什麼要用UIAlertView或UIAlertController?三、如何使用UIAlertView和UIAlertController?四、閱讀提醒。
一、UIAlertView與UIAlertController是什麼東東?
一句話,在所有行動裝置 App中,出現的提示窗一般都由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:alertControllerWithTitleUIAlertController *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以一種模組化替換的方式來代替這兩這兩個控制項的功能和作用。