質問

I would like the keyboard to appear automatically when a certain UITextView appears. Right now, the first time the UITextView appears, the user has to tap on it for the keyboard to appear. Subsequent appearances of the UITextView automatically make the keyboard appear. How can I make this happen the first time the UITextView appears as well?

-(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];
}

Basically, the first time this method is called the user has to tap on self.textView for the keyboard to appear, and I want the keyboard to appear automatically when the method is called, like it is on subsequent calls of this method.

役に立ちましたか?

解決

You should put the code

[self.textView becomeFirstResponder]

after you have put the text view into the controller's view.

他のヒント

Hope to help you

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

In iOS 6, -[UITextField becomeFirstResponder] doesn't work in -viewWillAppear: (comment to question by https://stackoverflow.com/users/1162777/anton)

Try following line when you want to active the textView.

[self.textView becomeFirstResponder];
    [UIView animateWithDuration:0.0 animations:^{
        [self addSubview:textview];
    } completion:^(BOOL finished){
        [textview becomeFirstResponder];
    }];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top