UIAlertView上怎么获取UITextField的内容

2025-04-06 02:11:28
推荐回答(1个)
回答1:

首先创建一个UIAlertView并且在上面添加一个UITextField
在UIAlertView上添加一个UITextField
Objective-C

UIAlertView *pwdAlert = [[UIAlertView alloc] initWithTitle:message
message:@"\n\n"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定", nil];
[pwdAlert setTag:kAlertTagOfJoinForumPassword];
[pwdAlert setDelegate:self];

UITextField *textFieldPwd = [[UITextField alloc] initWithFrame:CGRectMake(27.0, 60.0, 230.0, 25.0)];
[textFieldPwd setBackgroundColor:[UIColor whiteColor]];
[textFieldPwd setTag:kTextFieldTagOfForumPassword];
[textFieldPwd setPlaceholder:@"请输入密码"];
[pwdAlert addSubview:textFieldPwd;
[textFieldPwd release];

[pwdAlert show];
[pwdAlert release];

接下来获取UITextField里面输入的值,这里首先要实现UIAlertView的delegate的方法:alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

Objective-C

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSInteger aTag = [alertView tag];
//输入的密码
if(aTag == kAlertTagOfJoinForumPassword){
if (buttonIndex == 1) {
//选择确定
UITextField *pwdTextField = (UITextField *)[alertView viewWithTag:kTextFieldTagOfForumPassword];
NSString *pwd = [pwdTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// .....
}else{
//选择取消
}
}
}

Categorized under: iphone/ipad, UIAlertView.
Tagged with: UIAlertView, UITextField, 输入.
Comments are closed.