2013-12-19 48 views
0

我有一个iOS应用程序,我最近更新了它来处理UIAlertView/SubView问题,该问题导致文本框呈现为清晰或白色(或根本不渲染,不知道哪个) 。无论如何,这是一个相对简单的问题,因为我对Obj-C很陌生,但是如何从应用中的另一个调用中获取新文本框的值?在iOS应用程序中从UIAlertView文本框中检索值

这里是我的UIAlertView中:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Password" 
         message:@"Enter your Password\n\n\n" 
         delegate:self 
         cancelButtonTitle:@"Cancel" 
         otherButtonTitles:@"Login", nil]; 
alert.frame = CGRectMake(0, 30, 300, 260); 

它曾经被存储为一个的UITextField,然后加入到U​​IAlertView中作为一个子视图:

psswdField = [[UITextField alloc] initWithFrame:CGRectMake(32.0, 65.0, 220.0, 25.0)]; 
    psswdField.placeholder = @"Password"; 
    psswdField.secureTextEntry = YES; 
    psswdField.delegate = self; 
    psswdField.tag = 1; 
    [psswdField becomeFirstResponder]; 
    [alert addSubview:psswdField]; 
    [alert show]; 
    [alert release]; 

这一切现在注释掉,并相反,我把它改写为:

alert.alertViewStyle = UIAlertViewStyleSecureTextInput; 

这是我如何检索值:

[psswdField resignFirstResponder]; 
[psswdField removeFromSuperview]; 

activBkgrndView.hidden = NO; 
[activInd startAnimating]; 
[psswdField resignFirstResponder]; 
[self performSelectorInBackground:@selector(loadData:) withObject:psswdField.text]; 

现在我有点困惑,我如何从该文本框的值发送到loadData。

回答

5

您不想将自己的文本字段添加到警报视图。你不应该直接添加子视图到UIAlertView。 UIAlertView上有一个alertViewStyle属性,您想将其设置为UIAlertViewStyleSecureTextInput,它将为您添加一个文本字段。所以,你会用这样的行来设置它:

alert.alertViewStyle = UIAlertViewStyleSecureTextInput; 

然后,你将用委托方法- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex,你必须添加到类,你设置为你的UIAlertView中委托检索该文本字段中的值。下面是该代理方法的一个示例实施:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    // Make sure the button they clicked wasn't Cancel 
    if (buttonIndex == alertView.firstOtherButtonIndex) { 
     UITextField *textField = [alertView textFieldAtIndex:0]; 

     NSLog(@"%@", textField.text); 
    } 
} 
+0

对,有道理。正如你在我的问题中看到的,我已经在做第一部分。我想我需要帮助的是如何实际检索文本字段的示例。再说一遍,我是iOS新手,而且我更像是一个C#人员,而不是obj-c。我可以解决它,但我还不是很熟练。 – optionsix

+0

我真正需要帮助的是:我假设alert.alertViewStyle = UIAlertViewStyleSecureTextInput部分创建一个输入文本框。方法didDismissWithButtonIndex在按下按钮后调用该操作来对该文本框执行某些操作。之后是我朦胧的地方,我如何从委托方法中的文本框中获取数据? – optionsix

+0

我刚刚添加了委托方法的示例实现,当您关闭对话框时,该方法会将文本字段中的值打印到控制台。所以你可以看看你如何看待文本字段。 – Gavin