detecting if User pressed Go or hide keyboard butt

2020-04-10 05:34发布

问题:

I am making a browser type app..

Now i have made the Url entering textfield...which shows up the keyboard..

Now i have made it be Url type keyboard..it has Go button and for iPad a button to hide the keyboard..

-(BOOL)textFieldShouldReturn:(UITextField *)textField

i know above is the method that is called when user clicks return on Keyboard.. but for iPad ..there are two different things..i want to go the new URL if user presses Go..and leave the window as it is if user presses hide keyboard..but the problem is both the events report

-(BOOL)textFieldShouldReturn:(UITextField *)textField

so how do i differentiate user pressed Go ..or just hide keyboard..

回答1:

When the user taps Hide button, the UIKeyboardWillHideNotification gets called,so you should register for observing it

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardWillHideHandler:)
                                         name:UIKeyboardWillHideNotification
                                       object:nil];


- (void) keyboardWillHideHandler:(NSNotification *)notification {
    //show another viewcontroller here
}


回答2:

Apparently now it seems that the Go button only reports

-(BOOL)textFieldShouldReturn:(UITextField *)textField

and hide keyboard button doesn't..

I am pretty sure yesterday this was not the case..but anyways..i believe i am only to blame for this mess up..i will leave the question open if anyone needs clearing.



回答3:

You try to use touch function with key board I think it will work.



回答4:

At least on iOS 12 textFieldDidEndEditing(_:) is called when tapping the hide button (on iPad).

Hope this helps.