【iPhone開發】如何做一個Username/Password彈跳視窗(UIAlertView)?

  1. 首先,你要初始化一個UIAlertView物件。然後你必須將message的參數值設為@”\n\n\n”,這是用來多留幾行空白以便擺放輸入框欄位。
  2. 建立兩個UITextField物件,將他們加至UIAlertView裡成為subview。別忘了設定輸入框的背景色(setBackgroundColor)、預設文字(setPlaceholder)、安全表示(setSecureTextEntry,輸入文字時會變成星號,用於密碼欄)。
  3. 最後,在呈現UIAlertView時,在垂直位移加上一點Shift值並且有需要的話將之設定為FirstResponder。

Source Code:

UITextField *textField;
UITextField *textField2;

UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Username and password"
                                                 message:@"\n\n\n" // IMPORTANT
                                                delegate:nil
                                       cancelButtonTitle:@"Cancel"
                                       otherButtonTitles:@"Enter", nil];

textField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)];
[textField setBackgroundColor:[UIColor whiteColor]];
[textField setPlaceholder:@"username"];
[prompt addSubview:textField];

textField2 = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 260.0, 25.0)];
[textField2 setBackgroundColor:[UIColor whiteColor]];
[textField2 setPlaceholder:@"password"];
[textField2 setSecureTextEntry:YES];
[prompt addSubview:textField2];

// set place
[prompt setTransform:CGAffineTransformMakeTranslation(0.0, 110.0)];
[prompt show];
[prompt release];

// set cursor and show keyboard
[textField becomeFirstResponder];

Reference:
http://iphone-dev-tips.alterplay.com/2009/12/username-and-password-uitextfields-in.html

Tags: , , ,

Leave a Reply