1

我希望键盘在某个出现时自动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

4 回答 4

3

你应该把代码

[self.textView becomeFirstResponder]

将文本视图放入控制器视图之后。

于 2012-12-18T05:31:38.210 回答
2

希望能帮到你

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

在 iOS 6 中,-[UITextField becomeFirstResponder] 在 -viewWillAppear 中不起作用:(对https://stackoverflow.com/users/1162777/anton的问题的评论)

于 2013-01-17T16:19:22.730 回答
0

当您想激活 textView 时,请尝试以下行。

[self.textView becomeFirstResponder];
于 2012-12-18T04:56:17.117 回答
-1
    [UIView animateWithDuration:0.0 animations:^{
        [self addSubview:textview];
    } completion:^(BOOL finished){
        [textview becomeFirstResponder];
    }];
于 2014-04-20T13:32:19.260 回答