I've tried to make a toolbar that uses a UIButton like I created below but it does not seem to work. So now I'm trying add the button onto my UIView after the toolbar has been added. The problem now is that the TouchesBegan method does not pick up whenever I "click" the UIButton. Is there an easy way to connect a button directly to a method? If not, how do I get this button to respond to screen touches?
That's how I originally thought it was handled. When I tried this code:
UIButton *infoDarkButtonType = [[[UIButton buttonWithType:UIButtonTypeInfoLight] target:self action:@selector(doStuff)] autorelease];
I got this error in the console:
-[UIButton target:action:]: unrecognized selector sent to instance 0x4554e0'
Okay, so I tried going a different route. I created a UIView with a button on it to handle the touchesBegan method. But when the button is there it doesn't respond. If I remove the button then it does. Can anyone provide a clear example of how to respond just to a button click without the UI Builder?
#import "InfoButtonView.h"
@implementation InfoButtonView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Set the background to clear so we can see the background image when we flip
self.backgroundColor = [UIColor clearColor];
// Add the info button
infoLightButtonType = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
infoLightButtonType.frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
[self addSubview:infoLightButtonType];
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
printf("info button was just pressed");
}
The fastest, easiest way to do this is to download the Apple example code. They have over 30 examples on the iphone development area. In there are plenty of UIButton examples where you can copy the code directly. It shows how to setup the action and targets.
I suspect your method not found because you are missing this method or it's spelled incorrectly or has or doesn't have a : at the end.
You're also autoreleasing buttonWithType. Don't do that. It's already set to be autoreleased. If you didn't alloc or copy then the only real option to consider is Retain if you need to.