2009-05-07 6 views
7

AppStoreアプリのように内部にテキストボックスを持つalertviewを表示することは可能ですか? このようなダイアログでパスワードを尋ねます。 私はそれを使って他にいくつかのサードパーティのアプリを少なくとも2つ見てきました。プライベートAPIですか?iPhoneのテキストボックスでアラートビューを表示

答えて

9

はい、文書化されていません。 UIAlertViewにテキストフィールドを追加するには、addTextFieldWithValue:label:メソッドを使用します。最初の引数としてデフォルトテキストを呼び出し、空のフィールドに2番目の引数として表示するテキストを呼び出します。これを済ませたら、textFieldAtIndex:n - を使ってフィールドにアクセスできます。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Who are you?"  
         message:@"Give your full name" 
         delegate:self cancelButtonTitle:@"Cancel" 
         otherButtonTitles:@"OK", nil]; 
[alert addTextFieldWithValue:@""label:@"Name"]; 

// Customise name field 
UITextField* name = [alert textFieldAtIndex:0]; 
name.clearButtonMode = UITextFieldViewModeWhileEditing; 
name.keyboardType = UIKeyboardTypeAlphabet; 
name.keyboardAppearance = UIKeyboardAppearanceAlert; 
[alert show]; 

次のスニペットは、名前フィールドの値を取得する方法を示しています。

NSLog("Name is %@", [[modalView textFieldAtIndex:0] text]); 
+5

、この意志最近あなたはアプリストアから蹴ってください。 – zoul

+0

合意 - これはずっと前に書かれたものです。私は答えを削除する必要があります。 –

+0

@ JaneSales:私は答えを削除しませんが、私は間違いなくあなたの記事の一番上に大きな、目立つ免責事項を追加します。 – FreeAsInBeer

3

ジェフラマルシュがちょうどこれを行うには、いくつかのsample code on his blogを掲載しました。私はそれを試したときに書式はちょっとばかり見えましたが、それはおそらく良い出発点です。

16

ここでは、Tharindu MadushanaからApple認定の方法で対応しています。 http://iosdevelopertips.com/undocumented/alert-with-textfields.html

// Ask for Username and password. 
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Twitter Details!" message:@"\n \n \n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 

// Adds a username Field 
UITextField *utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; 
utextfield.placeholder = @"Username"; 
[utextfield setBackgroundColor:[UIColor whiteColor]]; 
[alertview addSubview:utextfield]; 

// Adds a password Field 
UITextField *ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 80.0, 260.0, 25.0)]; 
ptextfield.placeholder = @"Password"; 
[ptextfield setSecureTextEntry:YES]; 

[ptextfield setBackgroundColor:[UIColor whiteColor]]; [alertview addSubview:ptextfield]; 
// Move a little to show up the keyboard 
CGAffineTransform transform = CGAffineTransformMakeTranslation(0.0, 80.0); 
[alertview setTransform:transform]; 

// Show alert on screen. 
[alertview show]; 
[alertview release]; 

//... 
// Don't forget to release these after getting their values 
[utextfield release]; 
[ptextfield release]; 

をそして最後にこれは本当に古い答えと本当に古い質問にある

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) 
     return; //Cancel 

    UITextField *field = (UITextField *)[[alertView subviews] lastObject]; 
    NSLog (@"%@", field.text); 
} 
+0

私は個人的にトランスフォームを削除し、alertviewを解放した後に[utextfield becomeFirstResponder]を追加して、キーボードが自動的にポップアップするようにします。 – AlBeebe

5

戻ってテキストを取得するために:私は、このページでは、彼のコメントからそれを得ました。

これは私がIOS 5以降UIAlertViewへのUITextFieldを取得する方法のサンプルです:

alertTextFieldは、次のように設定された
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"New List Name" message:@"" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Continue", nil]; 

    message.alertViewStyle = UIAlertViewStylePlainTextInput; 
    self.alertTextField = [message textFieldAtIndex:0]; 
    self.alertTextField.keyboardType = UIKeyboardTypeAlphabet; 
    message.delegate = self; 
    [message show]; 
    [self.alertTextField becomeFirstResponder]; 

:他の人がすでに述べたように

@property (nonatomic, strong) UITextField *alertTextField; 
関連する問題