iOS9提示框的正確使用方式
在從iOS8到iOS9的升級(jí)過(guò)程中,彈出提示框的方式有了很大的改變,在Xcode7 ,iOS9.0的SDK中,蘋(píng)果已經(jīng)明確提示不再推薦使用UIAlertView,而推薦使用UIAlertController,現(xiàn)在,我們通過(guò)代碼來(lái)演示一下。
#import "LoginViewController.h" @interface LoginViewController () @property (weak, nonatomic) IBOutlet UITextField *passWord; @property (weak, nonatomic) IBOutlet UITextField *userName; @property (weak, nonatomic) IBOutlet UIButton *login; - (IBAction)loginOnClick:(UIButton *)sender; @end @implementation LoginViewController - (void)viewDidLoad { [super viewDidLoad]; //獲取通知中心 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; //注冊(cè)通知 [center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.userName]; [center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.passWord]; } -(void)textChange { //當(dāng)用戶名框和密碼框同時(shí)有內(nèi)容時(shí),登錄按鈕才可以點(diǎn)擊 self.login.enabled = (self.userName.text.length > 0 && self.passWord.text.length > 0); } //點(diǎn)擊登錄按鈕執(zhí)行的事件 - (IBAction)loginOnClick:(UIButton *)sender { if ([self.userName.text isEqual: @"xiaojin"] && [self.passWord.text isEqual: @"123456"]) { NSLog(@"successful"); [self performSegueWithIdentifier:@"loginIdentifier" sender:nil]; } else { //iOS9以前經(jīng)常用來(lái)創(chuàng)建提示框的方法 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用戶名或密碼出現(xiàn)錯(cuò)誤" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil]; [alert show]; } } @end
編寫(xiě)上述代碼時(shí),會(huì)有下列的警告提示:
說(shuō)明UIAlertView首先在iOS9中被棄用(不推薦)使用。讓我們?nèi)ビ肬IAlertController。但是運(yùn)行程序,發(fā)現(xiàn)代碼還是可以成功運(yùn)行,不會(huì)出現(xiàn)crash。當(dāng)輸入用戶名或密碼錯(cuò)誤時(shí)就會(huì)淡出提示框,如圖:
但是在實(shí)際的工程開(kāi)發(fā)中,我們有這樣一個(gè)“潛規(guī)則”:要把每一個(gè)警告(warning)當(dāng)做錯(cuò)誤(error)。所以為了順應(yīng)蘋(píng)果的潮流,我們來(lái)解決這個(gè)warning,使用UIAlertController來(lái)解決這個(gè)問(wèn)題。代碼如下:
#import "LoginViewController.h" @interface LoginViewController () @property (weak, nonatomic) IBOutlet UITextField *passWord; @property (weak, nonatomic) IBOutlet UITextField *userName; @property (weak, nonatomic) IBOutlet UIButton *login; - (IBAction)loginOnClick:(UIButton *)sender; @end @implementation LoginViewController - (void)viewDidLoad { [super viewDidLoad]; //獲取通知中心 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; //注冊(cè)通知 [center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.userName]; [center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.passWord]; } -(void)textChange { //當(dāng)用戶名框和密碼框同時(shí)有內(nèi)容時(shí),登錄按鈕才可以點(diǎn)擊 self.login.enabled = (self.userName.text.length > 0 && self.passWord.text.length > 0); } //點(diǎn)擊登錄按鈕執(zhí)行的事件 - (IBAction)loginOnClick:(UIButton *)sender { if ([self.userName.text isEqual: @"xiaojin"] && [self.passWord.text isEqual: @"123456"]) { NSLog(@"successful"); [self performSegueWithIdentifier:@"loginIdentifier" sender:nil]; } else { //初始化提示框; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用戶名或密碼出現(xiàn)錯(cuò)誤" preferredStyle: UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //點(diǎn)擊按鈕的響應(yīng)事件; }]]; //彈出提示框; [self presentViewController:alert animated:true completion:nil]; } } @end
看,這樣就不會(huì)有警告了吧!編譯運(yùn)行后的界面和上面的一樣。其中preferredStyle這個(gè)參數(shù)還有另一個(gè)選擇:UIAlertControllerStyleActionSheet。選擇這個(gè)枚舉類(lèi)型后,實(shí)現(xiàn)效果如下:
可以發(fā)現(xiàn)這個(gè)提示框是從底部彈出的。是不是很簡(jiǎn)單呢?通過(guò)查看代碼還可以發(fā)現(xiàn),在提示框中的按鈕響應(yīng)不再需要delegate委托來(lái)實(shí)現(xiàn)了。直接使用addAction就可以在一個(gè)block中實(shí)現(xiàn)按鈕點(diǎn)擊,非常方便。
總結(jié),可以發(fā)現(xiàn)這里我們呈現(xiàn)一個(gè)對(duì)話框使用了presentViewController這個(gè)方法,這個(gè)方法是呈現(xiàn)模態(tài)視圖(Modal View)的方法,也就是是說(shuō),此時(shí)的提示框是一個(gè)模態(tài)視圖。當(dāng)我們?cè)谶M(jìn)行界面跳轉(zhuǎn)的時(shí)候,也一般使用這個(gè)方法,此時(shí)呈現(xiàn)的第二個(gè)ViewController也是一個(gè)模態(tài)視圖。我們可以把模態(tài)視圖理解為一個(gè)浮動(dòng)在原先視圖上的一個(gè)臨時(shí)性的視圖或者界面,當(dāng)在模態(tài)視圖中調(diào)用dismissViewController方法時(shí),會(huì)返回上一個(gè)界面,并銷(xiāo)毀這個(gè)模態(tài)視圖對(duì)象。
以上就是本文的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- iOS移動(dòng)端(H5)alert/confirm提示信息去除網(wǎng)址(URL)
- iOS自定義提示彈出框?qū)崿F(xiàn)類(lèi)似UIAlertView的效果
- iOS自定義alertView提示框?qū)嵗窒?/a>
- iOS自定義推送消息提示框
- Android仿IOS自定義AlertDialog提示框
- iOS實(shí)現(xiàn)圓角箭頭矩形的提示框
- iOS實(shí)現(xiàn)UITableView數(shù)據(jù)為空時(shí)的提示頁(yè)面
- iOS微信分享后關(guān)閉發(fā)送成功提示并返回應(yīng)用
- IOS開(kāi)發(fā)實(shí)現(xiàn)手機(jī)震動(dòng)的提示實(shí)例代碼
- iOS自定義圓形進(jìn)度提示控件
相關(guān)文章
Objective-C的NSOperation多線程類(lèi)基本使用指南
這篇文章主要介紹了Objective-C的NSOperation多線程類(lèi)基本使用指南,談到了Operations的執(zhí)行順序和并發(fā)量等設(shè)置操作,需要的朋友可以參考下2016-02-02iOS簡(jiǎn)單登錄LoginViewController、注冊(cè)RegisterViewController等功能實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了iOS簡(jiǎn)單登錄LoginViewController、注冊(cè)RegisterViewController、UcenterViewController功能實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09iOS中使用Fastlane實(shí)現(xiàn)自動(dòng)化打包和發(fā)布
Fastlane是一套使用Ruby寫(xiě)的自動(dòng)化工具集,用于iOS和Android的自動(dòng)化打包、發(fā)布等工作,可以節(jié)省大量的時(shí)間。下面給大家介紹ios fastlane 自動(dòng)化打包和發(fā)布的安裝方法,需要的朋友參考下吧2017-05-05iOS開(kāi)發(fā)中UISwitch按鈕的使用方法簡(jiǎn)介
這篇文章主要介紹了iOS開(kāi)發(fā)中UISwitch按鈕的使用方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11Objective-C實(shí)現(xiàn)自定義的半透明導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了Objective-C實(shí)現(xiàn)自定義的半透明導(dǎo)航的相關(guān)資料,需要的朋友可以參考下2016-05-05