I know to send a parameter to a selector, we use withObject, but how to handle the following case:
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:[[NavigatorUtil new] autorelease] action:@selector(back:) ];
The back method looks like:
#import "NavigationUtil.h"
@implementation NavigationUtil
+(void) back: (UINavigationController*) navigationController
{
[navigationController popViewControllerAnimated:YES];
}
@end
Now I need to send a parameter to this selector? How to?
What do you mean “send a parameter to this selector”? This phrase doesn’t compute.
It seams to me you should RTFM a little, for example the Event Handling Guide for iOS.
In short, the button’s “action” is a message that the button sends to its target. In Objective-C, a message is defined by its selector, which can more or less be seen as the method name and signature.
The button knows about three signatures for this selector: zero, one or two arguments. In your case, you use a method with one argument. You can’t decide what it’s going to be. This argument is always the same. It’s going to be the sender, i.e. the button itself.
So that’s problem #1 with your action method.
Problem #2 with your action method is that you defined it as a class method. It should be an instance method.
Now finally, if you want to reach the UINavigationController from the NavigatonUtil, you need to find another way. For example to store it beforehand as an instance variable.