2012-03-08 5 views
29

更新UIAlertViewUIAlertViewすなわちは、どのように私はUIAlertViewにテキストフィールドを初期化することができます

alert.alertViewStyle = UIAlertViewStylePlainTextInput; 

これがうまく機能でテキスト入力フィールドを可能にするスタイルを持っているが、私はdefualtと入力テキストを初期化したいです"sample"のようなテキスト。

は、私が過去に人々が(素晴らしい作品)のような文書化されていないAPIを使用していることを

[alert addTextFieldWithValue:@"sample text" label:@"Text Field"]; 

を見たが、これはまだAppleの公式パブリックAPIではありませんので、私はそれを使用することはできません。

他の方法でこれを処理できますか? willPresentAlertViewで初期化しようとしましたが、テキストフィールドは読み取り専用のようです。

おかげで

答えて

8

テストされていないが、私はこれがうまくいくと仮定します。

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:...]; 
UITextField* textField = [alert textFieldAtIndex:0]; 
textField.text = @"sample"; 
[alert show]; 
57

UIALertViewが希望UITextFieldオブジェクトを返すtextFieldAtIndex:方法があります。デフォルトの設定を行い

UIAlertView *alert = .... 
UITextField *textField = [alert textFieldAtIndex:0]; 
textField.placeholder = @"your text"; 

UIAlertView Class Reference

+0

グレートを行う簡単な方法 - あなたはそれがあなたの質問に答えた場合、あなたは次の目盛りをクリックして答えを受け入れることができ歓迎:)だ – timeview

+1

を働いているあなたに感謝それには – Mutix

+0

これはもう動作していないようです。 textFieldAtIndexはこのエラーを生成します:***キャッチされない例外 'NSInvalidArgumentException'のためアプリを終了します、理由: 'textFieldIndex(0)はテキストフィールドの配列の範囲外です'。 – Symmetric

6

UIAlertViewStylePlainTextInputについては

は、テキストフィールドのインデックスは0です

あなたはその後、プレースホルダ(またはテキスト)を設定することができますテキストフィールドのプロパティですuiAlertViewの値は、このことが動作します。

UIAlertView *alert = .... 
UITextField *textField = [alert textFieldAtIndex:0]; 
[textField setText:@"My default text"]; 
[alert show]; 
0
- (IBAction)showMessage:(id)sender { 
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Add New Category" 
                 message:nil 
                delegate:self 
              cancelButtonTitle:@"Add" 
              otherButtonTitles:@"Cancel", nil]; 
    [message setAlertViewStyle:UIAlertViewStylePlainTextInput]; 
    [message show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 
    if([title isEqualToString:@"Add"]) 
    { 
     UITextField *username = [alertView textFieldAtIndex:0]; 
     NSLog(@"Category Name: %@", username.text); 
    } 
} 
9

UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"your title" message:@"your message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
    alerView.alertViewStyle = UIAlertViewStylePlainTextInput; 
    [[alerView textFieldAtIndex:0] setPlaceholder:@"placeholder text..."]; 
    [alerView show]; 
+0

Perfect Answer :) –

関連する問題