[分享]iOS開發-實現帶輸入框的AlertView及設置鍵盤樣式的方法

對於帶輸入框的彈出框(UIAlertView),在IOS5.0及以上版本,有一種較爲簡單的實現方式,即設置UIAlertView的alertViewStyle屬性便可。php

可供設置的屬性以下:ui

typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
    UIAlertViewStyleDefault = 0,
    UIAlertViewStyleSecureTextInput,
    UIAlertViewStylePlainTextInput,
    UIAlertViewStyleLoginAndPasswordInput
};

UIAlertViewStyleDefault,爲默認值,不帶輸入框
UIAlertViewStyleSecureTextInput爲密碼型輸入框,輸入的字符顯示爲圓點兒
UIAlertViewStylePlainTextInput爲明文輸入框,顯示輸入的實際字符
UIAlertViewStyleLoginAndPasswordInput爲用戶名,密碼兩個輸入框,一個明文,一個密碼。spa

取得輸入框指針的方法以下:
對於UIAlertViewStyleSecureTextInput和UIAlertViewStylePlainTextInput兩種狀況,
UITextField *tf = [alert textFieldAtIndex:0]便可取到輸入框指針,而後能夠進行具體的操做,包括設置鍵盤樣式
對於UIAlertViewStyleLoginAndPasswordInput,除了上面的輸入框,依次類推,還能夠取到第二個輸入框,即:
UITextField *tf2 = [alert textFieldAtIndex:1]指針

設置鍵盤樣式的方法,即設置UITextField的keyboardType屬性。具體值以下:code

typedef NS_ENUM(NSInteger, UIKeyboardType) {
    UIKeyboardTypeDefault,                // Default type for the current input method.
    UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
    UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
    UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
    UIKeyboardTypeNumberPad,              // A number pad (0-9). Suitable for PIN entry.
    UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).
    UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number.
    UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).
#if __IPHONE_4_1 <= __IPHONE_OS_VERSION_MAX_ALLOWED
    UIKeyboardTypeDecimalPad,             // A number pad with a decimal point.
#endif
#if __IPHONE_5_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
    UIKeyboardTypeTwitter,                // A type optimized for twitter text entry (easy access to @ #)
#endif
 
    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
 
};

示例代碼:ip

- (IBAction)pressed:(id)sender
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"message"
                                                        message:@"please input"
                                                       delegate:nil
                                              cancelButtonTitle:@"cancel"
                                              otherButtonTitles:@"OK", nil];
        // 基本輸入框,顯示實際輸入的內容
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        // 用戶名,密碼登陸框
    //    alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
        // 密碼形式的輸入框,輸入字符會顯示爲圓點
    //    alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
         
        //設置輸入框的鍵盤類型
        UITextField *tf = [alert textFieldAtIndex:0];
        tf.keyboardType = UIKeyboardTypeNumberPad;
         
        UITextField *tf2 = nil;
        if (alert.alertViewStyle == UIAlertViewStyleLoginAndPasswordInput) {
            // 對於用戶名密碼類型的彈出框,還能夠取另外一個輸入框
            tf2 = [alert textFieldAtIndex:1];
            tf2.keyboardType = UIKeyboardTypeASCIICapable;
        }
         
        // 取得輸入的值
        NSString* text = tf.text;
        NSLog(@"INPUT:%@", text);
        if (alert.alertViewStyle == UIAlertViewStyleLoginAndPasswordInput) {
            // 對於兩個輸入框的
            NSString* text2 = tf2.text;
            NSLog(@"INPUT2:%@", text2);
        }
         
        [alert show];
    }

分享來源:
http://bluevt.org/?p=103
http://www.cocoachina.com/bbs...ci

相關文章
相關標籤/搜索