6 Replies Latest reply: Aug 27, 2008 1:55 PM by Scott Squires1
darkpegasus Level 1 Level 1 (0 points)
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)
  • Scott Squires1 Level 3 Level 3 (910 points)
    You need to set an action (method) and a target (object) either in code or in interface builder.
  • darkpegasus Level 1 Level 1 (0 points)
    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'
  • darkpegasus Level 1 Level 1 (0 points)
    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
  • Scott Squires1 Level 3 Level 3 (910 points)
    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.
  • darkpegasus Level 1 Level 1 (0 points)
    Thanks, but I've looked at all of the examples and I haven't found a single one that handles a simple UIButton programmatically through code.
  • Scott Squires1 Level 3 Level 3 (910 points)
    Check UICatalog. I'd be surprised if all the rest of the samples are completely IB based now.