Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

TouchesBegan not working for all controls?

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?

UIButton *infoDarkButtonType = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
infoDarkButtonType.frame = CGRectMake(285.0, yPlacement, 25.0, 25.0);

MacBook Pro, Mac OS X (10.5.4)

Posted on Aug 26, 2008 9:53 PM

Reply
6 replies

Aug 27, 2008 9:53 AM in response to darkpegasus

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");
}


- (void)drawRect:(CGRect)rect {
// Drawing code
}


- (void)dealloc {
[super dealloc];
}


@end

Aug 27, 2008 11:47 AM in response to darkpegasus

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.

TouchesBegan not working for all controls?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.