从警告框中检索输入的文本



OK,我试图从这个示例http://junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html中检索这个文本字段的变量。他说要这样做:要输入文本,您只需要为文本字段和警报设置委托,如上面的示例代码所示。然后,您可以使用textFieldDidEndEditing:来获取值并将其临时存储在某个地方。当alertView: did解散withbuttonindex:被调用时,你可以查找保存的值,并使用它或丢弃它取决于什么按钮被按下。问题是我对iOS和objective - c都很陌生,所以这对我来说毫无意义。对我来说,文本域委托被设置为self。passwordfield。delegate = self;有人能举个例子吗?所以我可以看到如何检索输入的文本。

UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:@"Phone Number" message:@"nnn"
                                                       delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel",nil) otherButtonTitles:NSLocalizedString(@"OK",nil), nil];
UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(12,40,260,25)];
passwordLabel.font = [UIFont systemFontOfSize:16];
passwordLabel.textColor = [UIColor whiteColor];
passwordLabel.backgroundColor = [UIColor clearColor];
passwordLabel.shadowColor = [UIColor blackColor];
passwordLabel.shadowOffset = CGSizeMake(0,-1);
passwordLabel.textAlignment = UITextAlignmentCenter;
passwordLabel.text =@"Cell Phone Number xxx-xxx-xxxx";
[passwordAlert addSubview:passwordLabel];
UIImageView *passwordImage = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"passwordfield" ofType:@"png"]]];
passwordImage.frame = CGRectMake(11,79,262,31);
[passwordAlert addSubview:passwordImage];
UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)];
passwordField.font = [UIFont systemFontOfSize:18];
passwordField.backgroundColor = [UIColor whiteColor];
passwordField.secureTextEntry = YES;
passwordField.keyboardAppearance = UIKeyboardAppearanceAlert;
passwordField.delegate = self;
[passwordField becomeFirstResponder];
[passwordAlert addSubview:passwordField];
[passwordAlert setTransform:CGAffineTransformMakeTranslation(0,9)];
[passwordAlert show];
[passwordAlert release];
[passwordField release];
[passwordImage release];
[passwordLabel release];

在同一个类中创建textFieldDidEndEditing:委托方法:

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSString *textValue = textField.text;
    NSLog(@"Value: %@", textValue);
}

最新更新