从警报框中检索输入的文本。

时间:2011-08-24 15:58:20

标签: iphone objective-c ios

好的我正在尝试从此示例http://junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html中的此文本字段中检索变量。他说这样做: “要输入文本,您只需要为文本字段和警报设置委托,如上面的示例代码所示。然后您可以使用textFieldDidEndEditing:获取值并将其存储在临时位置。当alertView:didDismissWithButtonIndex时:如果被调用,您可以查找保存的值,并根据按下的按钮使用它或丢弃它。“  事情是我对iOS和目标c这么新,这对我来说毫无意义。对我来说,文本字段委托设置为self-- passwordField.delegate = self;有没有人有展示的例子?所以我可以看到如何检索输入的文本。

UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:@"Phone Number" message:@"\n\n\n"
                                                       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];

1 个答案:

答案 0 :(得分:1)

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

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