0

I have one textfield and textview in the view. I want to show toolbar on keyboard when my editing text in textView but I don't want to show toolbar while editing textField. I'm using below code:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    [super viewWillAppear:animated];

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(keyboardWillShow:)
               name: UIKeyboardWillShowNotification object:nil];
    [nc addObserver:self selector:@selector(keyboardWillHide:)
               name: UIKeyboardWillHideNotification object:nil];
 return YES;
}

and

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [[NSNotificationCenter defaultCenter] removeObserver:self    name:UIKeyboardWillShowNotification
                                              object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification
                                              object:nil];

    return YES;
}

My problem is when user trying to edit textfield and directly start editing textView we are not able to show toolBar for it then? How can I show toolbar on keyboard for such situation?

Hrishikesh Pol
  • 87
  • 1
  • 10
  • btw, why are you calling `viewWillAppear` method in your `textViewShouldBeginEditing` method ? – limon Apr 15 '13 at 10:02

3 Answers3

1

As it's explained before on this answer. UITextField and UITextView has a property inputAccessoryView for >iOS3.2, you can set any view you want, and it appears at the top of the keyboard. So, you don't need to use UINotificationCenter. Here is the code to accomplish what you want.

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIToolbar *keyboardToolbar =[[UIToolbar alloc] initWithFrame:CGRectMake(0,250,320,30)];
    keyboardToolbar.barStyle = UIBarStyleBlackOpaque;
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(dismissKeyboard)];
 
    UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    NSArray *items = [[NSArray alloc] initWithObjects:flex,barButtonItem, nil];
    [keyboardToolbar setItems:items];
    self.textView.inputAccessoryView =keyboardToolbar;
}

-(void)dismissKeyboard
{
    [self.textView resignFirstResponder];
}

All you need to do is set inputAccessoryView for your UITextView, so for UITextField default keyboard will appear.

I hope this will help.

Community
  • 1
  • 1
limon
  • 3,222
  • 5
  • 35
  • 52
0

You should use this delegate method to retrieve keyboard notification for UITextField or UITextView

- (void)keyboardWillShow:(NSNotification *)notification
{
    //self.keyboardNotification = notification; //store notification and process on text begin delegate method. 
}
Mani
  • 17,549
  • 13
  • 79
  • 100
  • Yes I have implemented this method but even this delegate method is not getting called when user switch from textField to textView directly as keyboard is already shown. – Hrishikesh Pol Apr 15 '13 at 10:16
  • 1
    where did you set `delegate`? check with your xib or `self.textView.delegate = self ` – Mani Apr 15 '13 at 10:24
  • thanks mani... that i was forget to add but inputAccessoryView help my problem and it also help me to optimise my code. – Hrishikesh Pol Apr 15 '13 at 10:35
0

You know already that you should use inputAccessoryView, but here is why your code does not work:

UIKeyboardWillShowNotification will be posted after textFieldShouldBeginEditing: has returned.

This is because you can cancel the editing by returning NO from textFieldShouldBeginEditing: , and in this case there should be no UIKeyboardWillShowNotification.

Don't remove the notification in textFieldShouldBeginEditing:. Otherwise you are no longer observing this notification when it is posted.

If you add the notifications in viewWillAppear:, remove them in viewWillDisappear:.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247