日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)

發(fā)布時間:2025/6/15 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
本文轉(zhuǎn)載至?http://blog.csdn.net/liuwuguigui/article/details/39494597 IOS8UIAlertViewUIActionSheet

iOS8推出了幾個新的“controller”,主要是把類似之前的UIAlertView變成了UIAlertController,這不經(jīng)意的改變,貌似把我之前理解的“controller”一下子推翻了~但是也無所謂,有新東西不怕,學(xué)會使用了就行。接下來會探討一下這些個新的Controller。

?

- (void)showOkayCancelAlert {NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];// Create the actions.UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");}];UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {NSLog(@"The \"Okay/Cancel\" alert's other action occured.");}];// Add the actions.[alertController addAction:cancelAction];[alertController addAction:otherAction];[self presentViewController:alertController animated:YES completion:nil]; }

這是最普通的一個alertcontroller,一個取消按鈕,一個確定按鈕。

新的alertcontroller,其初始化方法也不一樣了,按鈕響應(yīng)方法綁定使用了block方式,有利有弊。需要注意的是不要因為block導(dǎo)致了引用循環(huán),記得使用__weak,尤其是使用到self。

上面的界面如下:

如果UIAlertAction *otherAction這種otherAction多幾個的話,它會自動排列成如下:

另外,很多時候,我們需要在alertcontroller中添加一個輸入框,例如輸入密碼:

這時候可以添加如下代碼:

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {// 可以在這里對textfield進(jìn)行定制,例如改變背景色textField.backgroundColor = [UIColor orangeColor];}];

而改變背景色會這樣:

完整的密碼輸入:

- (void)showSecureTextEntryAlert {NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];// Add the text field for the secure text entry.[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {// Listen for changes to the text field's text so that we can toggle the current// action's enabled property based on whether the user has entered a sufficiently// secure entry.[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];textField.secureTextEntry = YES;}];// Create the actions.UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {NSLog(@"The \"Secure Text Entry\" alert's cancel action occured.");// Stop listening for text changed notifications.[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];}];UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {NSLog(@"The \"Secure Text Entry\" alert's other action occured.");// Stop listening for text changed notifications.[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];}];// The text field initially has no text in the text field, so we'll disable it.otherAction.enabled = NO;// Hold onto the secure text alert action to toggle the enabled/disabled state when the text changed.self.secureTextAlertAction = otherAction;// Add the actions.[alertController addAction:cancelAction];[alertController addAction:otherAction];[self presentViewController:alertController animated:YES completion:nil]; }

注意四點:

1.添加通知,監(jiān)聽textfield內(nèi)容的改變:

// Add the text field for the secure text entry.[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {// Listen for changes to the text field's text so that we can toggle the current// action's enabled property based on whether the user has entered a sufficiently// secure entry.[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];textField.secureTextEntry = YES;}];

2.初始化時候,禁用“ok”按鈕:

otherAction.enabled = NO;

self.secureTextAlertAction = otherAction;//定義一個全局變量來存儲

3.當(dāng)輸入超過5個字符時候,使self.secureTextAlertAction = YES:

- (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification {UITextField *textField = notification.object;// Enforce a minimum length of >= 5 characters for secure text alerts.self.secureTextAlertAction.enabled = textField.text.length >= 5; }

4.在“OK”action中去掉通知:

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {NSLog(@"The \"Secure Text Entry\" alert's other action occured.");// Stop listening for text changed notifications.[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];}];

?

最后是以前經(jīng)常是alertview與actionsheet結(jié)合使用,這里同樣也有:

- (void)showOkayCancelActionSheet {NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);NSString *destructiveButtonTitle = NSLocalizedString(@"OK", nil);UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];// Create the actions.UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {NSLog(@"The \"Okay/Cancel\" alert action sheet's cancel action occured.");}];UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {NSLog(@"The \"Okay/Cancel\" alert action sheet's destructive action occured.");}];// Add the actions.[alertController addAction:cancelAction];[alertController addAction:destructiveAction];[self presentViewController:alertController animated:YES completion:nil]; }

在底部顯示如下:

?

好了,至此,基本就知道這個新的controller到底是怎樣使用了。

總結(jié)

以上是生活随笔為你收集整理的iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。