BloonsTowerDefence
BloonsTowerDefence

Reputation: 1204

Calling UIKeyboard method on UITextView and not UITextFields

I have view with a UITextView for comments on the bottom. Since its on the bottom of the page, whenever someone edits it, a method is called to shift the view of the page, so that you can still see the comment box. My problem is that the same method also gets called when user are editing UITextFields.

Here's what I have. First I declare a notification:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardWillShow:) 
                                             name:UIKeyboardWillShowNotification 
                                           object:self.view.window];

Then the method itself

- (void)keyboardWillShow:(NSNotification *)notif{

...

}

My first thought was to add a condition, to check and see if the object was a TextView, and only then execute the code. But since I am not passing the object to the method, is there anyway to tell the method what type of object I am dealing with

Upvotes: 1

Views: 1244

Answers (1)

rdelmar
rdelmar

Reputation: 104082

Text fields and text views also send notifications. In the textFieldShouldBeginEditing and the textViewShouldBeginEditing implementations you could set a flag that you can read in your implementation of the keyboardWillShow method -- the keyboard notification is sent after the text field or text view notifications.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    self.sender = @"text field";
    return YES;
}


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    self.sender = @"text view";
    return YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification 
                                               object:self.view.window];

}

- (void)keyboardWillShow:(NSNotification *)notif{
    NSLog(@"%@",self.sender);
}

Upvotes: 2

Related Questions