1

私はメッセージベースのiPhoneアプリに取り組んでいます。受信したメッセージに返信する画面があります。この画面には、bottomTextView と topTextView のような 2 つの UITextView が含まれています。

topTextView は、bottomTextView の InputAccessory ビューとして追加されます

ユーザーが画面に入ると、topTextViewFirstResponder になる必要があります。表示されていますが、カーソルがtopTextViewに配置されていません。カーソルは textview にありbottomTextViewます。カーソルを配置してtopTextViewを最初の応答者にする方法は?

これが私が試したコードです:

- (void)viewDidLoad
{
    [super viewDidLoad];            
    bottomBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
    bottomBarViewImage.image = [UIImage imageNamed: @"toolbarbg~iphone.png"];
    [bottomBarView addSubview: bottomBarViewImage];
    [self.view addSubview: bottomBarView];

    bottomTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 7.5, 210, 25)];
    bottomTextView.delegate = self;
    bottomTextView.backgroundColor = [UIColor clearColor];
    bottomTextView.font = [UIFont fontWithName:@"Helvetica" size:14];
    bottomTextView.scrollEnabled = NO;
    [bottomBarView addSubview: bottomTextView];

    topBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
    topBarViewImage.image = [UIImage imageNamed: @"toolbarbg~iphone.png"];
    [topBarView addSubview: topBarViewImage];

    topTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 7.5, 210, 25)];
    topTextView.delegate = self;
    topTextView.backgroundColor = [UIColor clearColor];
    topTextView.font = [UIFont fontWithName:@"Helvetica" size:14];
    topTextView.scrollEnabled = NO;
    [topBarView addSubview: topTextView];

    [bottomTextView becomeFirstResponder];
    [bottomTextView setInputAccessoryView: topBarView];
}

-(void) textViewDidBeginEditing:(UITextView *)textView
{
    if(textView == bottomTextView)  
    {
        bottomTextView.scrollEnabled = NO;

        [topTextView becomeFirstResponder];
    }
}

topTextViewwithが表示されtopBarViewていますが、カーソルが topTextView に配置されていません。この問題を解決するのを手伝ってくれませんか? 前もって感謝します。

4

1 に答える 1

2

[topTextView becomeFirstResponder];UITextView の delegateを呼び出しているためだと思いますtextViewDidBeginEditing:。したがって、編集を開始すると、topTextView のみがファーストレスポンダーになりますbottomTextView。viewDidLoad[topTextView becomeFirstResponder];の代わりに呼び出してみてください。[bottomTextView becomeFirstResponder];それがどうなるか見てください。よくわかりませんが、becomeFirstResponder電話しないかもしれませんtextViewDidBeginEditing:。うまくいくかどうかはわかりませんが、試してみる価値はあります...

編集 :

ここで関連する問題を見つけました。textView がすぐに表示されないため、ファーストレスポンダーになれない可能性があります。@Tom による受け入れられた回答は次のとおりです。

私の解決策:キーボード(およびアクセサリビュー)がいつ表示されたかを確認してください!

ステップ 1) 通知をリッスンします (通知を受け取る前に、このコードが読み取られていることを確認してください)。

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(changeFirstResponder)
                                             name:UIKeyboardDidShowNotification 
                                           object:nil];

ステップ 2) キーボードが表示されたら、inputaccessoryview のテキストフィールドをファーストレスポンダーに設定できます。

-(void)changeFirstResponder
{
    [textField becomeFirstResponder]; //will return TRUE;
}
于 2012-09-17T12:08:04.710 に答える