2013-12-19 88 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