开发者

Call a function (IBAction) in a class with a button in another class (and view)

开发者 https://www.devze.com 2023-02-01 06:51 出处:网络
I have an application where I used a UITabBarController with 3 buttons. So I also have 3 classes. What I want to do is to c开发者_运维技巧all an - (IBAction) doSomething: (id) sender {} in class 1 (vi

I have an application where I used a UITabBarController with 3 buttons. So I also have 3 classes. What I want to do is to c开发者_运维技巧all an - (IBAction) doSomething: (id) sender {} in class 1 (view 1) with a button in class 2 (view 2).


Take whatever it is that your doSomething method (not function) does and use it to create method in a new class. Both controllers can import the class, instantiate it, and use the method.

Alternately you can send a notification to whichever controller has doSomething, but if the code in the method really does apply to both controllers, provide it to both controllers.


You can have one controller send a notification to another. When you want to notify class 1 to perform the button-pressed code you'll send out a notification like this:

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

You don't have to call it ABCPerformButtonAction, you just need a string that you'll recognize and something -- I used ABC because I don't know your initials or the name of the app or whatever -- to help ensure you don't accidentally send a notification that has the same name as a notification something you're unaware of is listening for (including 3rd party libraries you're using, etc.).

When that notification goes out, any object that has registered with the defaultCenter to listen for @"ABCPerformButtonAction" will perform any actions you choose. Here's how controller 1 registers (this should be located in some place like ViewDidLoad or the initialization method of the object):

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

The selector there, performDoSomething:, is just the name of a method you want to run when the notification goes out. That method has to have a specific format, so you can't call your doSomething method directly. It would look like this:

- (void)performDoSomething:(NSNotification *)notif {
    [self doSomething];
}

As you can see, all it does is call the method. Obviously it could do much more, and you can even send information along with the notification (see below).

Lastly, it's important that you also remove your object as an observer before it's deallocated. In your Dealloc method of each object that registered to receive the notification you add this:

[[NSNotificationCenter defaultCenter] removeObserver:self];

Hopefully that makes sense. The Apple documentation for NSNotificationCenter explains more and they provide several sample apps that use notifications.

0

精彩评论

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