UITextView没有响应另一个UITextView附件查看iPhone?

时间:2012-09-17 11:58:46

标签: iphone ios uitextview becomefirstresponder

我正在开发基于消息的iPhone应用程序。我有一个屏幕来回复收到的消息。此屏幕包含两个UITextViews,如bottomTextView和topTextView。

topTextView被添加为bottomTextView的InputAccessory视图

当用户进入屏幕时, topTextView必须成为FirstResponder 。它显示但光标未放在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];
    }
}

topTextView显示topBarView,但光标未放置在topTextView中。你能帮我解决这个问题吗?提前谢谢。

1 个答案:

答案 0 :(得分:2)

我认为可能是因为您在UITextView的委托[topTextView becomeFirstResponder];中调用了textViewDidBeginEditing:。因此,当您开始编辑bottomTextView时,topTextView仅成为第一响应者。尝试在viewDidLoad中调用[topTextView becomeFirstResponder];而不是[bottomTextView becomeFirstResponder];。看看它如何。我不确定,但becomeFirstResponder可能不会致电textViewDidBeginEditing:。不确定它会起作用,但值得一试......

编辑:

我发现了一个相关的问题here。可能是因为textView没有立即出现,所以它不能成为第一响应者。以下是@Tom接受的答案:

  

我的解决方案:检查键盘何时(以及配件视图)   出现了!

     

步骤1)监听通知(确保读取此代码   在你想收到通知之前)。

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

步骤2)当键盘出现时,您可以设置文本字段   您的inputaccessoryview成为第一响应者:

-(void)changeFirstResponder
{
    [textField becomeFirstResponder]; //will return TRUE;
}