tags:

views:

8832

answers:

6

I am trying to add an info button to my app to provide custom help.

Instead of adding the button to the nib and linking the event (touchUpInside) to the controller, I decided to add the button programmatically. The button shows up. When I add the target event handler to be executed when the button is touched, it does not work. That my method(doHelp) is not being called on touching the button.

When I debugged it, the event is not registered with the button! Although the code does not throw any exceptions.

Here is the code snippet FROM the view:

 // Create a Button to get Help  
 UIButton *helpButton =  [UIButton buttonWithType:UIButtonTypeInfoDark ] ;
 buttonRect = helpButton.frame;

 // CALCulate the bottom right corner
 buttonRect.origin.x = rect.size.width - buttonRect.size.width - 8;
 buttonRect.origin.y = rect.size.height - buttonRect.size.height - 8; 
 [helpButton setFrame:buttonRect];

 [helpButton addTarget:self action:@selector(doHelp:) forControlEvents:UIControlEventTouchUpInside];
 [helpButton setEnabled:TRUE];
 [self addSubview:helpButton];

........

// Another METHOD ELSEWHERE in the VIEW object
-(void)doHelp:(id)Sender
{
    [self setHelpNeeded:TRUE];
    [self setNeedsDisplay];
}

What am I doing wrong please? I have looked at the SDK help and samples and am really flummoxed! Am hoping another pair of eyes will help! :-)

This code snippet is in the View Object in case you need to know. I just added the doHelp to help the first 2 responders... thanks.

**UPDATE 6/4/09 ** - I have been trying all night and nothing worked. I think there is something wrong in the way I have set up the method selector as my method never gets called. Everything else looks fine. Even using a NIB file does not work. I have tagged the button, retrieved it and added the method selector but to no avail. There is something fundamental which I am doing wrong... Argh!!! Any ideas, anyone?

A: 

It's been awhile, but I think your addTarget needs to take the object that contains the doHelp: selector, like so:

[helpButton addTarget:self action:@selector(doHelp:)];

assuming somewhere in that same View you have:

- (void)doHelp: { }

passing nil to addTarget means that you're sending that selector to no recipient.

Jonas
my bad... in my code I do have self in the target. It still does not work. I was just trying the nil option as the doc says, it will send the action up the responder chain if nil is specified in target.
Master Chief
My Target method is <pre>-(void)doHelp:(id)Sender{ [self setHelpNeeded:TRUE]; [self setNeedsDisplay];}</pre>Is there anything wrong with the method signature?
Master Chief
A: 

The problem is your addTarget:nil there. The selector you gave it for action is just a message it'll send to its target. You didn't give it a target, so it doesn't know what to do with that message. You probably want to pass in self instead of nil there.

zem
U R Right. Even sending Self as the target does not do it. :-(
Master Chief
+4  A: 

Resolved it finally!!! and learnt something in return. Did cost me a few days to figure this out.

The reason my UIButton object was not working was because I found that in case of a UIIMageView object:

"initWithImage: This method adjusts the frame of the receiver to match the size of the specified image. It also disables user interactions for the image view by default."

AND my UIButton had been assigned as a subview of a UIImageView control !!!

There was no errors / warnings. It just gets disabled quietly.

Solution: Created a container UIView object which now contains the UIImageView AND the button so that the button appears as overlayed on the Image but it is actually a sibling of the image and a subview of the dummy container UIView.

Master Chief
A: 

I came across this while googling for a solution to the same problem. At least with the 3.x SDK, all you have to do is set the UserInteractionEnabled property of the UIImageView to YES.

Thanks for posting your discovery about the problem, I wouldn't have even thought to look at that one.

jduggins42
A: 

Master Chief,

thanks so much for posting this...although the solution can be this or enabling the user interaction of UIImageView, it is easy to forget this. I was myself getting puzzled when I found your post..thanks !

Warm regards, Abhinav

Abhinav
A: 

I had a similar problem where Buttons were outside of the view and did not receive tap messages

what helps is to set background colour of the parent view, to see that button is outside of it:

...
[buttonParentView addSubview: myButton];
buttonParentView.backgroundColor = [UIColor cyanColor];