對于帶輸入框的彈出框(UIAlertView),在IOS5.0及以上版本,有一種較為簡單的實現(xiàn)方式,即設(shè)置UIAlertView的alertViewStyle屬性即可。
可供設(shè)置的屬性如下:
typedefNS_ENUM(NSInteger, UIAlertViewStyle) {UIAlertViewStyleDefault=0,? ? UIAlertViewStyleSecureTextInput,? ? UIAlertViewStylePlainTextInput,? ? UIAlertViewStyleLoginAndPasswordInput};
UIAlertViewStyleDefault,為默認值,不帶輸入框
UIAlertViewStyleSecureTextInput為密碼型輸入框,輸入的字符顯示為圓點兒
UIAlertViewStylePlainTextInput為明文輸入框,顯示輸入的實際字符
UIAlertViewStyleLoginAndPasswordInput為用戶名,密碼兩個輸入框,一個明文,一個密碼。
取得輸入框指針的方法如下:
對于UIAlertViewStyleSecureTextInput和UIAlertViewStylePlainTextInput兩種情況,
UITextField *tf = [alert textFieldAtIndex:0]即可取到輸入框指針,然后可以進行具體的操作,包括設(shè)置鍵盤樣式
對于UIAlertViewStyleLoginAndPasswordInput,除了上面的輸入框,依次類推,還可以取到第二個輸入框,即:
UITextField *tf2 = [alert textFieldAtIndex:1]
設(shè)置鍵盤樣式的方法,即設(shè)置UITextField的keyboardType屬性。具體值如下:
typedefNS_ENUM(NSInteger, UIKeyboardType){? ? UIKeyboardTypeDefault,// Default type for the current input method.UIKeyboardTypeASCIICapable,// Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain activeUIKeyboardTypeNumbersAndPunctuation,// 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_ALLOWEDUIKeyboardTypeDecimalPad,// A number pad with a decimal point.#endif#if__IPHONE_5_0 <= __IPHONE_OS_VERSION_MAX_ALLOWEDUIKeyboardTypeTwitter,// A type optimized for twitter text entry (easy access to @ #)#endifUIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,// Deprecated};
示例代碼:
- (IBAction)pressed:(id)sender? ? {? ? ? ? UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"message"message:@"please input"delegate:nilcancelButtonTitle:@"cancel"otherButtonTitles:@"OK",nil];// 基本輸入框,顯示實際輸入的內(nèi)容alert.alertViewStyle= UIAlertViewStylePlainTextInput;// 用戶名,密碼登錄框//? ? alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;// 密碼形式的輸入框,輸入字符會顯示為圓點//? ? alert.alertViewStyle = UIAlertViewStyleSecureTextInput;//設(shè)置輸入框的鍵盤類型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];? ? }