Simple #selector Syntax Error - iphone

This seems to be the simplest of problems... yet I can't figure out what's syntactically incorrect with this snippet.
Running this:
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:appDelegate action:#selector(addImage:NO)];
Returns:
Expected '(' before ')' token
I've looked it over hundreds of times, yet I can't find what's wrong with it. Thanks for any assistance.

You can't have NO in your #selector() directive.
Be careful. UI Actions selectors should have the method signature:
- (void)myMethod:(id)sender;
and not
- (void)myMethod:(BOOL)someBool;
You may need a wrapper method:
- (void)doneAction:(id)sender;
{
[appDelegate addAction:NO];
}

#selector() can't accept default values, change it to
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:appDelegate action:#selector(addImage:)];
Edit:
To make things clear, when you have
- (void)addImage:(BOOL)resize;
this is translated to "addImage:" as "method" name (selector) and when you pass over "addImage:NO" you are telling objc to look
- (void)addImage:(BOOL)resize NO:(BOOL)nod; // just for the example
ending up with wrong (invalid) selector, resulting in double back trace to all super objects calling respondsToSelector(addImage:NO) then throwing BAD_ACCESS.
p.s. Its more then year since I wrote my last line of objc.
Edit 2:
You can use wrapper as mentioned above or UIBarButtonItem.tag property
cancelButton.tag = 1; // 1 indicating addImage:NO
- (void) addImage:(id)sender {
UIBarButtonItem *button = (UIBarButtonItem *)sender;
if (button.tag == 1) {
// your value is NO
} else {
// your value is YES
}
}
but I would not go with this anyway. :)

Related

objective-c: Delegate object argument getting overwritten when i create multiple instances of custom class

EDIT: I apologize for wasting time, the erorr had nothing to do with what I'm taking about but rather some logic in my code that made me believe this was the cause. I'm awarding Kevin with the correct answer since using his idea to pass the whole AuthorSelectionView, and his note on correcting the NSNumer mistake. Sorry about that.
I've been trying to figure this out for hours, and even left it alone for a day, and still can not figure it out...
My situation is as follows:
I've created a custom class that implements 'UIView' and made this class into a protocol as follows:
custom UIView h file
#protocol AuthorSelectionViewDelegate <NSObject>
-(void)AuthorSelected:(NSNumber *)sender;
#end
#import <UIKit/UIKit.h>
#interface AuthorSelectionView : UIView
#property (nonatomic,assign) id<AuthorSelectionViewDelegate> delegate;
#property (strong,retain) NSNumber *authorID;
- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl withID:(int)authorID ;
#end
the implementation...
- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl withID:(int)authorID
{
self = [super initWithFrame:frame];
if (self) {
self.authorID = [[NSNumber alloc] initWithInt:authorID]; //used to distinguish multiple instances of this class in a view.
...
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, FRAMEWIDTH, FRAMEHEIGHT)];
[button addTarget:self action:#selector(CUSTOMBUTTONCLICK) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
return self;
}
- (void) CUSTOMBUTTONCLICK
{
[self.delegate performSelector:#selector(AuthorSelected:) withObject:self.authorID];
}
Now the method in my delegate object gets called just fine, but my major problem here is that something is going on with the object being pass through when i have multiple instances of the AuthorSelected class alloc'd.. (the NSNumber authorID). I'm getting some weird behavior with it. It seems almost random with the value being passed, but i'm detecting some pattern where the value passed through is coming up late..
thats confusing so ill try to explain:
I create two instances of the AuthorSelected view, one with authorID=1 and the other with authorID=2.
On the first press, lets say i press the first button, i'll get 1 as expected.
On the second press, if I press the 1st custom button, i'll get '1', but if i press the second i'll still get 1.
On the third go, either button will give me back '2'
I feel like this is some issue with pointers since that has always been a weak point for me, but any help would be greatly appreciated as I can not seem to figure this one out.
Thank you!
EDIT:
as requested here is how I create the AuthorSelectionView Objects...
AuthorSelectionView * asView01 = [[AuthorSelectionView alloc]
initWithFrame:CGRectMake(0, 0, FRAMEWIDTH, FRAMEHEIGHT)
withImage:userPic1
withLabel:randomUserName
withID:1];
asView01.delegate = self;
AuthorSelectionView * asView02 = [[AuthorSelectionView alloc]
initWithFrame:CGRectMake(0, 0, FRAMEWIDTH, FRAMEHEIGHT)
withImage:userPic2
withLabel:randomUserName2
withID:2];
asView02.delegate = self;
A detail that may be important:
As soon as i click on one of these custom views, my code is set to (for now) call the method that runs the above AuthorSelectionView alloc code, so that i can refresh the screen with the same layout, but with different userpic/userName. This is poor design, I know, but for now I just want the basic features to work, and will then worry about redrawing. I metion this tidbit, becuase I understand that objective-c 'layers' veiws on top of eachother like paint on a canvas, and had a thought that maybe when I click what I think may be my 2nd button, its really 'clicking' the layer beneath and pulling incorrect info.
Your description of the problem is a bit confusing, but this line in your init is very clearly wrong:
self.authorID = [self.authorID initWithInt:authorID];
In -init, your property self.authorID defaults to nil, so the expression [self.authorID initWithInt:authorID] is equivalent to [nil initWithInt:authorID], which evaluates back to nil. So you should actually be seeing nil in your action. You probably meant to say self.authorID = [NSNumber numberWithInt:authorID]
You're missing the alloc message, so this message:
self.authorID = [self.authorID initWithInt:authorID];
Is sent to a nil target, because self.authorID hasn't been allocated yet.
So first allocate it, then use the init method, or mix these two messages. A faster syntax allows to do it this way:
self.authorID= #(authorID);
EDIT
I don't see where you initialize the delegate, that method shouldn't even be called if you haven't initialized it. Show the code where you create the AuthorSelectionView objects and set the delegates.
instead of :
self.authorID = [self.authorID initWithInt:authorID];
put :
self.authorID = [NSNumber numberWithInt:authorID];
or
self.authorID = [[NSNumber alloc] initWithInt:authorID];
EDIT :
Don't you have errors or warnings in your code ? I can't see you returning self object in the init method ("return self;")

UIBarButtonItem created using initWithCustomView doesn't trigger action

I'm updating some old code, and to make more room in a toolbar, I'm converting the Buttons from test to images. An example of the new and old code in loadView is this:
// New code, doesn't work.
UIButton *toggleKeyboardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
toggleKeyboardBtn.bounds = CGRectMake( 0, 0, showKeyboardImage.size.width, showKeyboardImage.size.height );
[toggleKeyboardBtn setImage:showKeyboardImage forState:UIControlStateNormal];
UIBarButtonItem *toggleKeyboardItem = [[UIBarButtonItem alloc] initWithCustomView:toggleKeyboardBtn];
[toggleKeyboardItem setTarget:self];
[toggleKeyboardItem setAction:#selector(toggleKeyboard:)];
// Original code, works jut fine.
UIBarButtonItem *setupItem = [[[UIBarButtonItem alloc] initWithTitle:#"Setup" style:UIBarButtonItemStyleBordered target:[UIApplication sharedApplication].delegate action:#selector(showSetupView:)] autorelease];
My new code is copied from Cannot set action on UIBarButtonItem, and I'm fairly certain that I'm not making their mistake since my text button is working just fine.
showSetupView() is in my AppController.m file, and the setup screen appears and disappears as the button is pressed.
toggleKeyboard(), OTOH, is in the same file as the loadView() routine, and currently consists of this code:
//- (void)toggleKeyboard {
- (IBAction)toggleKeyboard:(id)sender {
NSLog(#"Entering toggleKeyboard()...");
hiddenKeyboard = !hiddenKeyboard;
[self prepareToolbarsAndStatusbar];
}
Needless to say, although I see the button-press animation, I never see the NSLog message. And one last observation, made by accident. Changing the setAction selector to this:
[toggleKeyboardItem setAction:#selector(noSuchRoutine:)];
compiles cleanly, possibly indicating that my routine name is being ignored for some reason.
Anyone have any ideas? Thanks.
I found the answer! In button action not responding iphone, it's said that the action and target need to be set on the UIButton, not the UIBarButtonItem. I don't know if that's new with the latest version of Xcode, but I guess it is since other questions (such as the one I mention above) use a different technique. Here's my new code:
UIButton *toggleKeyboardButton = [UIButton buttonWithType:UIButtonTypeCustom];
toggleKeyboardButton.bounds = CGRectMake( 0, 0, keyboardAddImage.size.width, keyboardAddImage.size.height );
[toggleKeyboardButton setImage:keyboardAddImage forState:UIControlStateNormal];
[toggleKeyboardButton addTarget:self action:#selector(toggleKeyboard) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *toggleKeyboardItem = [[UIBarButtonItem alloc] initWithCustomView:toggleKeyboardButton];
//[toggleKeyboardItem setTarget:self];
//[toggleKeyboardItem setAction:#selector(toggleKeyboard:)];
Though its too late, but for future references, I would like to quote apple docs for the method,
- (instancetype)initWithCustomView:(UIView *)customView;
The bar button item created by this method does not call
the action method of its target in response to user interactions.
Instead, the bar button item expects the specified custom view to
handle any user interactions and provide an appropriate response.
Did you try
-(void)toggleKeyboard
and
[toggleKeyboardItem setAction:#selector(toggleKeyboard)]; without :
and it made any difference? Is the method declared in the interface file?

EXC_BAD_ACCESS when trying to get selectedSegmentIndex of UISegmentedControl

I'm programmatically adding a UISegmentedControl to my UINavigationBar as follows
UISegmentedControl *toggleSwitch = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects: #"Bar", #"Scatter", nil]];
toggleSwitch.segmentedControlStyle = UISegmentedControlStyleBar;
[toggleSwitch addTarget:self action:#selector(toggleSwitched:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:toggleSwitch];
self.navigationItem.rightBarButtonItem = buttonItem;
[toggleSwitch release];
[buttonItem release];
I then have a method, toggleSwitched to handle the events:
-(void) toggleSwitched:(id) sender {
if([sender isKindOfClass:[UISegmentedControl class]]) {
NSLog(#"%#",((UISegmentedControl*)sender).selectedSegmentIndex );
}
}
From my reading of the Apple Documentation, this is the proper way to set up the event handling for the touch event.
HOWEVER, when I run the program (in the simulator) and tap the segments, the following things happen:
If it's the first segment (index 0), 'null' is logged to the console
If it's the second segment (index 1), the program crashes with EXC_BAD_ACCESS
When I look in the variables window of the debugger, sender is of type UISegmentedControl and the _selectedSegment property appears to be correct (0 or 1).
What am I improperly accessing and how can I set up my delegate function to switch between the two values of the UISegmentedController?
Your NSLog is wrong. selectedSegmentIndex returns NSUInteger, so you need to use #"%d" instead of #"%#". %# is for objects, so the system is trying to use the number as a memory address that points to a object, and generating an error for you.

iPhone/iPad - issues with NavigationBar button?

I my application i have added button in NavigationBar like this..
UIBarButtonItem *more=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"search-25by25.png"] style:UIBarButtonItemStylePlain target:self action:#selector(SelectMission:)];
self.navigationItem.rightBarButtonItem = more;
When i am clicking on button application get's shutdown...
If i am doing same thing with normal button it's working fine can any one help me why it's behaving like this?
Try This
UIImage *i=[UIImage
imageNamed:#"search-25by25.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.bounds = CGRectMake( 0, 0, i.size.width, i.size.height );
[myButton setImage:i forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(SelectMission:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *more=[[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem
= more;
hope it helps :)
Have you looked in the code for SelectMission:? The code you've posted is only for presenting the button, which from your description appears to be working.
Also if there's anything being dumped into the console (Command-Shift-R)?
Judging by the crash log in your comment, I would say this has nothing to do with the UIBarButtonItem class in particular, and everything to do with your action handler. The crash logs tell the whole story: Your class does not implement a method called SelectMission: that takes one argument. Some caveats about the #selector keyword that you will want to double check:
1) Capitalization. Make sure that the method you implement is SelectMission:. Not selectMission:, selectmission:, Selectmission:, etc.
2) Arguments. The colon indicates that the method SelectMission: takes one argument. If you have implemented it and forgotten the argument it will crash with the exception you posted.
That should help narrow down the issue.

Selector with argument

I have a method like this:
- (void)methodWithParameter:(id)parameter {
}
and I want to call it using an UIBarButtonItem
barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(methodWithParameter:)];
I want to specify the parameter but I can't use withObject: after action: because I get a warning:
No -initWithBarButtonSystemItem:target:action:withObject: method found
can anybody help me with this?
It doesn't work that way. You cannot pass a parameter to an action. An action method will always have either:
no arguments at all,
one argument (id)sender,
or two arguments (id)sender and (UIEvent *)event.