在没有用户点击UITextView的情况下调用getsFirstResponder

时间:2012-12-18 04:54:50

标签: objective-c ios uitextview becomefirstresponder

我希望键盘在出现某个UITextView时自动显示。现在,第一次UITextView出现时,用户必须点击它才能显示键盘。随后出现的UITextView会自动显示键盘。如何在UITextView第一次出现时才能实现这一目标?

-(void)displayComposeScreen
{
    [self.nameField resignFirstResponder]; //This is a different UITextView, but my issue doesn't change whether I leave this line in or take it out

    if (!self.textView)
    {
        self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 20, 280, 150)]; //This is the UITextView with whose appearance I want the keyboard to appear automatically.
        self.textView.font = [UIFont fontWithName:@"Helvetica Neue" size:14];
        self.textView.delegate = self;
    }
    [self.textView becomeFirstResponder];
    self.textView.hidden=NO;
    if (self.ghhaiku.userIsEditing==NO)
    {
         self.textView.text = @"";
    }
    else
    {
        self.textView.text = self.ghhaiku.text;
    }
    [self.view addSubview:self.textView];
}

基本上,第一次调用此方法时,用户必须点击self.textView才能显示键盘,并且我希望键盘在调用方法时自动显示,就像在后续调用时一样这种方法。

4 个答案:

答案 0 :(得分:3)

您应该输入代码

[self.textView becomeFirstResponder]
在之后

,您已将文本视图放入控制器的视图中。

答案 1 :(得分:2)

希望能帮到你

[textField performSelector:@selector(becomeFirstResponder) 
                withObject:nil 
                afterDelay:0.2f]

In iOS 6, -[UITextField becomeFirstResponder] doesn't work in -viewWillAppear:(对https://stackoverflow.com/users/1162777/anton提问题的评论)

答案 2 :(得分:0)

如果要激活textView,请尝试以下行。

[self.textView becomeFirstResponder];

答案 3 :(得分:-1)

    [UIView animateWithDuration:0.0 animations:^{
        [self addSubview:textview];
    } completion:^(BOOL finished){
        [textview becomeFirstResponder];
    }];