简体   繁体   中英

How can I prevent my search field from becoming the initial first responder in my popover?

I have a popover with a search field at the top of it. There's nothing else in the popover that could be the first responder. When the popover appears, the search field always ends up starting out as the first responder. I don't want this to happen.

I've tried:

  • Selecting "Refuses First Responder" for the search field. This seems to solve the problem, but when you click on the search field, the becoming-first-responder animation doesn't happen.

  • In viewDidLoad of my view controller:

     [self.view.window makeFirstResponder:nil]; 

    Doesn't do anything. Seems like the search field becomes the first responder after the view loads.

  • This did work, sort of:

     [self.view.window performSelector:@selector(makeFirstResponder:) withObject:nil afterDelay:0]; 

    But you can see the search field doing the resign-first-responder animation when the popover opens.

What can I do to prevent the search field from becoming first responder?

This should work (untested):

  • Create a subclass of the search field with a mayBecomeFirstResponder property
  • Change the class of your search field to that subclass
  • In that subclass overwrite - (BOOL)acceptsFirstResponder { return self. mayBecomeFirstResponder; } - (BOOL)acceptsFirstResponder { return self. mayBecomeFirstResponder; }
  • Add a search field outlet to your view controller and connect it
  • In the view controller overwrite - (void)viewWillAppear and set the search mayBecomeFirstResponder to NO
  • In the view controller overwrite - (void)viewDidAppear and set the search mayBecomeFirstResponder to YES

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM