1. 程式人生 > >IOS基礎UI之(五)UIAlertView、UIActionSheet和UIAlertController詳解

IOS基礎UI之(五)UIAlertView、UIActionSheet和UIAlertController詳解

     iOS 8的新特性之一就是讓介面更有適應性、更靈活,因此許多檢視控制器的實現方式發生了巨大的變化。比如說Alert Views、Action Sheets。 下面就大致介紹它們的使用方式。

    UIAlertView:

  1.建立UIAlertView。 UIAlertView的按鈕是水平排列的,當按鈕多的時候由於考慮的位置不夠,因此會垂直排列。

引數:delegate: 代理    otherButtonTitles: 其它按鈕,可以新增多個。多個用逗號隔開

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"標題" message:@"這個是UIAlertViewStyleDefault的預設樣式" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登入",nil ];

 2.設定對話方塊樣式。UIAlertView樣式有四種:

UIAlertViewStyleDefault  預設樣式

UIAlertViewStylePlainTextInput  可輸入文字樣式

UIAlertViewStyleSecureTextInput可輸入密碼樣式

UIAlertViewStyleLoginAndPasswordInput 可輸入文字和密碼

alert.alertViewStyle = UIAlertViewStyleDefault;

 3.顯示對話方塊
[alert show];
效果:


4。 監聽點選按鈕觸發事件。 

現在我們要需求是,使用者名稱和密碼不為空,登入按鈕才可點選。點選登陸按鈕控制檯輸入相應的使用者名稱和密碼。如何實現呢???

實現方法:實現UIAlertViewDelegate協議,呼叫響應對話方塊檢視的按鈕動作的回撥方法。還有當文字框內容改變時,呼叫alertViewShouldEnableOtherButton:方法可以讓按鈕動態地可用或者不可用。

  4.1 監聽對話方塊點選按鈕方法實現控制檯輸出:

/**
 *  alert按鈕監聽事件
 *
 *  @param alertView   alertview
 *  @param buttonIndex 按鈕下標(從0開始)
 */
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    //NSInteger num = [alertView numberOfButtons];//對話方塊的按鈕個數
   // NSLog(@"對話方塊共%ld個按鈕",num);
    if (buttonIndex == 1) {//登入按鈕
        NSString *name = [alertView textFieldAtIndex:0].text; //第一個輸入框的值
        NSString *password = [alertView textFieldAtIndex:1].text;//第二個輸入框的值
        NSLog(@"使用者名稱:%@,密碼:%@",name,password);
    }
}
 

  4.2 當文字框內容改變時,呼叫alertViewShouldEnableOtherButton:方法可以讓按鈕動態地可用或者不可用

/**
 *  當提示框的文字框內容改變時觸發
 *
 *  @param alertView alertView
 *
 *  @return 是否可用
 */
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView{
    BOOL isLogin = NO;
    NSString *name = [alertView textFieldAtIndex:0].text; //第一個輸入框的值
    NSString *password = [alertView textFieldAtIndex:1].text;//第二個輸入框的值
    if (name.length!=0 && password.length!=0) {
       isLogin = YES;
    }
    return isLogin;
}

                                                          

UIActionSheet

UIActionSheet是按鈕垂直排列的上拉選單。 ios8.3之後已經廢棄了。

NS_CLASS_DEPRECATED_IOS(2_0,8_3,"UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead")

 UIActionSheet使用方式很簡單。

  1.建立UIActionSheet。

按鈕樣式:常規(default)、取消(cancel)以及警示(destruective), 其中警示(destruective)會顯示紅色。

UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"退出" otherButtonTitles:@"版本更新",@"反饋", nil ];
  2.顯示對話方塊
[sheet showInView:self.view];
                              

UIAlertController

蘋果官方在ios8以後推薦使用UIAlertController。 UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一種模組化替換的方式來代替UIAlertView和UIActionSheet的功能和作用。是使用對話方塊(alert)還是使用上拉選單(action sheet),就取決於在建立控制器時設定首選樣式的。

UIAlertControllerStyleActionSheet  --->   UIActionSheet

 UIAlertControllerStyleAlert   ----> UIAlertView

 1.建立UIAlertController。 

     注意:UIAlertController無需指定代理,也無需在初始化過程中指定按鈕

 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"標題" message:@"這是預設樣式UIAlertControllerStyleActionSheet" preferredStyle:UIAlertControllerStyleActionSheet];

2.建立UIAlertAction,將動作按鈕新增到控制器上。UIAlertAction由標題字串、樣式以及當用戶選中該動作時執行的程式碼塊組成。UIAlertActionStyle三種動作樣式:常規(default)、取消(cancel)以及警示(destruective)。

  注意:取消按鈕是唯一的,如果添加了第二個取消按鈕,程式會執行時丟擲異常:

* Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘UIAlertController can only have one action with a style of UIAlertActionStyleCancel’

 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *loginout = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDestructive handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"反饋" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:cancelAction];
    [alert addAction:loginout];
    [alert addAction:okAction];

3.顯示
[self presentViewController:alert animated:YES completion:nil];

                                                          

  以上是UIAlertController實現UIActionSheet(不能實現可輸入)。下面通過UIAlertController實現UIAlertView,效果和上面UIAlertView一樣,可輸入文字和密碼,並且控制按鈕的狀態,控制檯輸入結果。

    1.建立。注意:樣式為UIAlertControllerStyleAlert

   UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登入視窗" message:@"請填寫登入資訊" preferredStyle:UIAlertControllerStyleAlert];

   2.建立UIAlertAction,將動作按鈕新增到控制器上
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

    UIAlertAction *login = [UIAlertAction actionWithTitle:@"登入" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        NSString *name =  alert.textFields.firstObject.text;
        NSString *pwd = alert.textFields.lastObject.text;
        NSLog(@"使用者名稱:%@,密碼:%@",name,pwd);
        //點選登陸按鈕後,銷燬觀察者物件
        [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
    }];
    login.enabled = NO;//登陸按鈕不可點選
    [alert addAction:cancel];
    [alert addAction:login];


3.新增可輸入框並且通知觀察者(notification observer)--UITextFieldTextDidChangeNotification,判斷更改按鈕狀態
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"使用者名稱";
        //通知觀察者
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(alertTextFiledDidChange:) name:UITextFieldTextDidChangeNotification object:textField ];
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"密碼";
        textField.secureTextEntry = YES;
         //通知觀察者
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFiledDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
    }];

4.接受訊息,更改按鈕狀態
/**
 *  觀察者訊息方法,更改按鈕的狀態
 *
 *  @param notification 訊息
 */
-(void)alertTextFiledDidChange:(NSNotificationCenter *)notification{
    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
    if(alertController){
        UIAlertAction *loginAction = alertController.actions.lastObject;
        NSString *name= alertController.textFields.firstObject.text;
        NSString *pwd = alertController.textFields.lastObject.text;
        loginAction.enabled = (name.length!=0 && pwd.length!=0);
    }
}

 5.顯示
 [self presentViewController:alert animated:YES completion:nil];


      文字至此!後面學習將繼續更新。。。。。。。。。。。。

相關推薦

IOS基礎UI()UIAlertViewUIActionSheetUIAlertController

     iOS 8的新特性之一就是讓介面更有適應性、更靈活,因此許多檢視控制器的實現方式發生了巨大的變化。比如說Alert Views、Action Sheets。 下面就大致介紹它們的使用方式。     UIAlertView:   1.建立UIAlertView

IOS基礎UI(三)手寫UIstoryboard方式實現圖片移動縮放

手寫UI是最早進行UI介面佈局的方法,優點是靈活自由,缺點是使程式碼看起來比較長。平時學習的時候可以多嘗試手寫ui,這樣會更深入熟悉控制元件。storyboard開發效率相對比較高。實際開發中看情況而定!!      下面用這兩種方式分別實現圖片移動和縮放。 功能描述:  

Linux基礎知識IO效能監控工具iostat命令

Linux系統出現了效能問題,一般我們可以通過top、iostat、free、vmstat等命令來檢視初步定位問題。其中iostat可以提供更豐富的IO效能狀態資料。 1. 基本使用 $iostat -d -k 1 10 引數 -d 表示,顯示裝置(磁碟)使用狀態;-k某些使用block為單位的列強

javascript技術難點(三)thisnewapplycall

4)    this、new、call和apply的相關問題   講解this指標的原理是個很複雜的問題,如果我們從javascript裡this的實現機制來說明this,很多朋友可能會越來越糊塗,因此本篇打算換一個思路從應用的角度來講解this指標,從這個角度理解this

Redis系列--7RedisTemplate Serializer

redistemplate serializer詳解<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionF

MySQL中tinytexttextmediumtextlongtext

空值 bin fcc val name 分辨 單選 objects 1.7 一、數字類型 類型 範圍 說明 Char(N) [ binary] N=1~255 個字元binary :分辨大小寫 固定長度 std_name cahr(32) not

touch事件中的touchestargetToucheschangedTouches

move htm nal 手指 tde hang 參數 eve function touches:當前屏幕上所有觸摸點的列表; targetTouches:當前對象上所有觸摸點的列表; changedTouches:涉及當前(引發)事件的觸摸點的列表: 可通過一個例子來區分

UARTSPII2C

運算 引入 下拉 初始 故障 服務 布線 發的 理由 做單片機開發時UART,SPI和I2C都是我們最經常使用到的硬件接口,我收集了相關的具體材料對這三種接口進行了詳細的解釋。 UART UART是一種通用串行數據總線,用於異步通信。該總線雙向通信,可以實現全雙工傳輸和接收

Android 自定義view(1) --- AttrStyleTheme

轉載:https://www.jianshu.com/p/dd79220b47dd 概念說明:       Attr:屬性,風格樣式的最小單元;      Style:風格,它是一系列Attr的集合用以定義一個View

Python爬蟲基礎:驗證碼的爬取識別

今天要給大家介紹的是驗證碼的爬取和識別,不過只涉及到最簡單的圖形驗證碼,也是現在比較常見的一種型別。 執行平臺:Windows Python版本:Python3.6 IDE: Sublime Text 其他:Chrome瀏覽器 簡述流程: 步驟1:簡單介紹驗證碼 步驟2:

本地儲存(一)—— CookieSessionStorageLocalStorage

目錄 1. Cookie 2. Web Storage 2.1 Session Storage 2.2 Local Storage 2.3 Web Storage 的瀏覽器支援情況 3. Cookie、SessionStorage和LocalStorage的對比

StringStringBuilderStringBuffer

以JDK1.8原始碼為例 一、原始碼 String: public final class String implements java.io.Serializable, Comparable<String>, CharSequence { …… }

TCPUDPHTTP

http:是用於www瀏覽的一個協議。 tcp:是機器之間建立連線用的到的一個協議。 1、TCP/IP是個協議組,可分為三個層次:網路層、傳輸層和應用層。 在網路層有IP協議、ICMP協議、ARP協議、RARP協議和BOOTP協議。 在傳輸層中有TCP協議與UDP協議。 在

【Java基礎】Jar包結構結構分析操作

一 JAR包結構分析 JAR(Java Archive FIle)Java歸檔檔案,是Java標準的文件格式,是一個或多個Java位元組碼檔案的打包壓縮檔案,採用常見的ZIP壓縮演算法,和ZIP檔案十分類似,可以直接解壓。 JAR檔案主要用來壓縮和釋

iOS 推薦快速製作app icon的神器步驟

由於公司沒有美工,所以app的icon圖示圖片,都要自己完成。這裡就說我覺得最快捷生成各種大小比例的icon。 1.畫出圖示 如果你的設計的圖示不復雜,就靠幾個形狀圖案就可以的完成的話,推薦下面兩種方法。 (1)PS:如果你會PS是最好的,利用向量圖,就可以任意伸縮大小,而

Opencv RotatedRect類中的pointsanglewidthheight

在Opencv中的影象處理中,經常要用到minAreaRect()函式求最小外接矩形, 該函式的返回值就是一個RotatedRect類物件。 RotatedRect類定義如下: class CV_EXPORTS RotatedRect { public: //! various

javascript中的_proto_constructorprototype

首先,在JavaScript中,任何物件都有一個proto屬性;任何方法都有prototype屬性,指向一個物件,稱為原型物件,這個物件有一個proto屬性,另外還有一個constructor屬性。 <script> function

Servlet學習筆記():ServletConfigServletContext

2)獲取:SevletConfig 物件中維護了 ServletContext 物件的引用,通過 SevletConfig.getServletContext()方法獲取ServletContext 物件。或者直接呼叫封裝好的getServletContext ()方法即可獲取。  3)作用:由於一個Web

【扯皮系列】一篇與眾不同的 StringStringBuilder StringBuffer

## 碎碎念 這是一道老生常談的問題了,字串是不僅是 Java 中非常重要的一個物件,它在其他語言中也存在。比如 **C++、Visual Basic、C# 等**。字串使用 String 來表示,字串一旦被創建出來就不會被修改,當你想修改 StringBuffer 或者是 StringBuilder,出於

iOS UI入門——使用Objective-CSwift實現警告檢視操作列表(UIAlertViewUIActionSheetUIAlertController

警告彈框和操作列表在開發中常用到,iOS9之後,UIAlertView和UIActionSheet都會報黃色的警告,但是還是依然可以使用的。在這裡主要介紹一在這三個控制元件在Objective-C和Swift下的使用程式碼。 Objective-C程式碼: #import "Vi