views:

21

answers:

1

Hi

I'm currently doing this to add a button to my navigation bar to call the SwitchViews method.

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back to App" 
                                                                  style:UIBarButtonItemStyleBordered
                                                                 target:nil
                                                                action:@selector(switchViews:)];
self.navigationItem.leftBarButtonItem=backButton;
[backButton release];

This method happens to be in another class (SwitchViewController) but it is working as I guess the method call goes through the responder chain until it finds it in SwitchViewController.

Is there any way that I can set the target to my class instead of nil?

Guess that would make the code easier to read, debug and maintain.

I'm sure it's a simple answer to some more experienced programmers than me...

Thanks

A: 

Setting target to nil should, in theory, not work at all, but maybe it does some intelligent stuff with it as you say. Setting target to nil apparently results in the action being sent up the responder chain. Didn't know that.

To set it to your class, you first need a reference to said class. Who holds that class? The application delegate? Could the application delegate hand that class's reference over to this class, since it needs it? If this class is initialized programmatically, you could even tweak the init method to take a new argument (the class).

What does the method which has the above code snippet in it look like? Is it an init method? A viewDidLoad method?

If it's an init method and it's called from a NIB file (i.e. it's initWithCoder:) then your best bet might be to simply grab the reference directly from the application delegate, using

((YOURAPPDELEGATE *)[[UIApplication sharedApplication] delegate]).THATCLASS

The above presumes that you've properly set up a @property for THATCLASS (which is your intended target), e.g.

@property (nonatomic, retain) ExampleClass *THATCLASS;
Kalle
Hej KalleNot sure I fully understand all of what you're saying (not your fault, I'm not just really up to speed yet). I have the class with the method imported in my .m file. It's a class without a nib that switches between two views and I never create an instance of it, just add it as a subview. According to Apple documentation: "If nil, the action message is passed up the responder chain where it may be handled by any object implementing a method corresponding to the selector"Thanks for your answer and question. Right now it's working, so I will live with that.Ha en trevlig sommar!
Structurer
Huh, I've actually never heard of the nil => responder chain behavior before. Never seen it either. Thanks for pointing that out, I'll update the answer. As for the original problem, you would simply have to maintain a reference to the SwitchViewController somewhere, or alternatively, you would find it by its superview's subviews array.
Kalle
Oh, and glad sommar på dig också! :)
Kalle