2016-05-12 5 views
0

を作成しているUITextViewに焦点を当て、私はUIAlertを作成し、動的ににUITextViewを追加します。はプログラムで私のiOSアプリでAccessoryViewとして

UIAlertView *tweetAlert = [[UIAlertView alloc] initWithTitle:@"Compose Tweet" 
                  message:nil 
                  delegate:self 
                cancelButtonTitle:@"Cancel" 
                otherButtonTitles:nil]; 
     tweetTextView = [UITextView new]; 
     [tweetTextView setText:[NSString stringWithFormat:@"%@ %@", [[NSUserDefaults standardUserDefaults] valueForKey:@"tweetText"], self.setShareURL]]; 

     [tweetAlert setValue:tweetTextView forKey:@"accessoryView"]; 
     [tweetAlert addButtonWithTitle:@"Tweet"]; 
     [tweetAlert show]; 
     [tweetTextView becomeFirstResponder]; // focus textview and open keyboard 

が、私はそのように最後の行にUITextViewに注力しようとUIAlertViewが表示されたらキーボードは開いています。ただし、キーボードが表示されず、UITextViewにフォーカスがありません。

私も

[[tweetAlert valueForKey:@"accessoryView"] becomeFirstResponder]; 

を試してみました。しかし、私はエラー

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIAlertView 0x1358b3200> valueForUndefinedKey:]: this class is not key value coding-compliant for the key accessoryView.' 

にUIAlertViewでaccessoryViewとして埋め込まれている動的に作成UITextView、に集中する方法任意のアイデアを得ますか?

+0

1) 'UIAlertView'はiOS 8以降で廃止されました。したがって、' UIAlertController'を使用する必要があります。 2) 'UIAlertView'と' UIAlertController'はカスタムコントロールを追加するように設計されていません。あなたは 'UITextView'をサポートするカスタムコントロールを作成したり見つける必要があります。 3)あなたが使用しようとしているこの 'accessoryView'は何ですか? 'UIAlertView'にはそのようなことはありません。おそらくあなたは 'UITableViewCell'について考えているでしょう。 – rmaddy

答えて

1

はあなたがファーストレスポンダになるのTextView作るためにUIAlertViewDelegate方法- (void)didPresentAlertView:(UIAlertView *)alertViewを使用することができます。あなただけのテキスト入力UIAlertViewを使用する必要があります。

- (void)didPresentAlertView:(UIAlertView *)alertView { 
    [tweetTextView becomeFirstResponder]; 
} 
+0

ありがとう、これは働いた! – scientiffic

1

通常、アラートビューの表示階層を変更することはお勧めできません。

UIAlertView *tweetAlert = [[UIAlertView alloc] initWithTitle:@"Compose Tweet" 
                  message:nil 
                  delegate:self 
                cancelButtonTitle:@"Cancel" 
                otherButtonTitles:nil]; 
tweetAlert.alertViewStyle = UIAlertViewStylePlainTextInput; 
+0

私は複数の行のテキストが必要なのでこれをしないことを選択しました – scientiffic

関連する問題