如何在UIAlertView中为用户标识和密码添加uitextfield?
问题描述:
我正在创建一个应用程序,我需要在UIAlertView中为用户登录名创建两个UITextField或TextBox用户ID和密码。我尝试了一些东西,但是在这里创建的textfield变得非常大,几乎覆盖了整个警报视图。请有人帮助我如何组织警报视图内的文本框和标签等。我需要 - 应该有一个标签为“用户ID”和“密码”的小文本框。那么该怎么做?请帮帮我。提前Thanx。如何在UIAlertView中为用户标识和密码添加uitextfield?
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"User Login" message:@"Please Enter the Following \n\n\n\n\n\n\n" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:@"Sign In", nil];
UILabel *lblUserName = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, 85, 21)];
lblUserName.tag =2;
//lblUserName.backgroundColor = [UIColor darkGrayColor];
lblUserName.font = [UIFont systemFontOfSize:15.0];
lblUserName.text = @"User Name:";
lblUserName.textColor=[UIColor whiteColor];
//[lblUserName setFont:[UIFont fontWithName:@"TimesNewRomanPS-boldMT" size:8]];
lblUserName.backgroundColor = [UIColor clearColor];
[alert addSubview:lblUserName];
UITextField *txtUserName;
//if(txtUserName==nil)
txtUserName = [[UITextField alloc] initWithFrame:CGRectMake(89, 50, 160, 30)];
txtUserName.tag = 3;
txtUserName.borderStyle = UITextBorderStyleBezel;
txtUserName.textColor = [UIColor whiteColor];
txtUserName.font = [UIFont systemFontOfSize:12.0];
txtUserName.placeholder = @"User ID";
[alert addSubview:txtUserName];
alert.tag=3;
[alert show];
[alert release];
答
从iOS5开始,UIAlertView支持使用Alert Style UIAlertViewStyleLoginAndPasswordInput的登录屏幕。在UIAlertView Class Reference
答
UIAlertView *alert =[UIAlertView alloc] init];
alert.delegate =self //make sure to add the UIAertViewDelegate in the .h file
alert.alertViewStyle =UIAlertViewStyleLoginAndPasswordInput;
alert.message [email protected]"Please login";
[alert show];
[alert release];
然后在clickedButtonAtIndex委托方法
的更多信息:
{
UITextField *username = [alertView textFieldAtIndex:0];
UITextfield *password = [alertView textFieldAtIndex:1];
NSLog(@"Username %"@ password %"@,username.text, password.text);
}