tags:

views:

66

answers:

2

This line of code is in AController.m

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
    target:self
    action:@selector(addItem)];

And - (void) addItem:(id) sender; is also in AController.m

If I want to call a method - (void) addItem1:(id) sender; in BController.m, how can I change to make it work? What should I pass to the target parameter?

Many thanks!

A: 

You should pass an instance of BContorller. If you want more please comment, and I'll see what I can do in 1hr when I'm free.

    BCont=[[BController alloc] init];
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
    target:BCont
    action:@selector(addItem)];

Try that.

Tom H
Thank you. One more question, what is the best way to organize the actions? I found there are too many similar methods in different controller, especially if I have multiple table and use Core Data, there will be a lot of similar code. Any suggestion?
sza
Hmmm... I'm not sure what you mean exactly, but creating a base class with all the similar methods, and subclassing it to add the different functionality might work.
Tom H
A: 

You have to pass a live instance of BController.

As an aside, an action method should technically have the form:

- (void) actionName:(id) sender;

Just the name with a sender parameter can work but sometimes the runtime gets snippy about it.

TechZen
I am no expert (so please correct me here), but I thought that that was only necessary for Mac OS X? If they're anything related to IBActions, that is the case, as without sender isn't ok on Mac os x, but is ok for iPhone.
Tom H
Not to my knowledge. The sender is important because you may have several different buttons that need to evoke the same action or the action may require info from the control. It really isn't related to Interface Builder at all. I've seen problems on iPhone with the improper form.
TechZen
Objective-C with its runtime binding can be tricky if you're not used to it. Naming conventions are very important to it unlike other languages for example the use of `- (AttributeType *) attributeName` for getters and `- (void) setAtributeName:(AttributeType *) aValue;` for setters. The runtime has no way to find those methods for the calls like `self.attributeName` or `instance.attributeName` except by the pattern of the selector.
TechZen