开发者

Can I use a static IBAction method for event hookups?

开发者 https://www.devze.com 2023-02-05 16:22 出处:网络
I have a multiview iPhone app I\'m developing that will have lots of inputs on different screens and instead of having to repeat myself all over the place with some method that just sends the resignFi

I have a multiview iPhone app I'm developing that will have lots of inputs on different screens and instead of having to repeat myself all over the place with some method that just sends the resignFirstResponder message, I thought I would make a static method in my root controller class and solve all my resignFirstReponder needs with a single function. Is that possible? I can't seem to drag the root controller .h file into the Interface builder and if I try to drop the class in there I can't hook any events up to it. Is there a way to hook events up to an IBAction method that is static?

Edit:

I should have been more clear in what I'm trying to accomplish. Basically, I call resignFirstResponder in the "Did End On Exit" event of text fields to get rid of the keyboard. (Is this even the correct way? I'm an iPhone newbie) Because I'm going to be using this all over the place on different views, I didn't want to have to write the same function for each view. I want to have a +(void) resignSomeKeyboardsOrSom开发者_如何转开发ething kind of function in my root controller that I can hook my "Did End On Exit" events up to from each view. Is there a way to do that? Sorry for the vagueness the first time around.


Let me see if I understand this, you have an application with multiple screens that do all sorts of things and you want these things to trigger a function, correct?

It sounds to me like notifications will work well for you.

So let's say you have a function, resignMyResponder, defined in your root view controller. Just add the following to your root view controller file, in your initialization function:

[[NSNotificationCenter defaultCenter] 
 addObserver:self 
 selector:@selector(resignMyResponder:) 
 name:@"resignThis" 
 object:nil];

Then, in your other screens/files, whenever you'd like resignMyResponder called, simply do:

[[NSNotificationCenter defaultCenter] 
 postNotificationName:@"resignThis" object:nil];

resignMyResponder will probably need to be pretty complex to handle all the different situations in which it might get called, but that depends on the implementation of your application.


UIResponder unfortunately isn't as clear as NSResponder is about this.

resignFirstResponder

Notifies the receiver that it has been asked to relinquish its status as first responder in its window.

- (BOOL)resignFirstResponder

Discussion

The default implementation returns YES, resigning first responder status. Subclasses can override this method to update state or perform some action such as unhighlighting the selection, or to return NO, refusing to relinquish first responder status.

If we group a class's instance methods into 2 categories (sorry, it's late, maybe this is oversimplifying but...):

1) Methods that are intended to be called directly from some other object to retrieve some data or to perform an action. [view setNeedsDisplay:YES];

2) Methods that are intended to be overridden by (concrete) subclasses. You generally never call these methods yourself in code; rather, they are called on your behalf by Cocoa with the intention that you override them to perform your custom implementation. Cocoa calls - (void)drawRect:(NSRect)frame; for you. This is especially the case in abstract classes like UIResponder and NSResponder.

- (BOOL)resignFirstResponder is of the latter: this method is essentially Cocoa asking you "Would you like to resign first responder?". You answer YES or NO. It is called automatically as needed. You don't need to call it directly.

The NSResponder documentation is even more clear:

resignFirstResponder

Notifies the receiver that it’s been asked to relinquish its status as first responder in its window.

- (BOOL)resignFirstResponder

Discussion

The default implementation returns YES, resigning first responder status. Subclasses can override this method to update state or perform some action such as unhighlighting the selection, or to return NO, refusing to relinquish first responder status.

Use the NSWindow makeFirstResponder: method, not this method, to make an object the first responder. Never invoke this method directly.

Not sure if there is an iPhone equivalent to makeFirstResponder:, but that is what you should striving for (you actively make the new view to be first responder).

0

精彩评论

暂无评论...
验证码 换一张
取 消