iOS - Dynamic Buttons
Asked Answered
P

3

6

I'm trying to use dynamic buttons created via code (no IB) in my project and they appear where and how I want them but they don't fire their actions.

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button1.frame = CGRectMake(475, 302, 49, 58);
    [button1 setTitle:@"1" 
             forState:(UIControlState)UIControlStateNormal];
    [button1 addTarget:self
                action:@selector(goToFirstTrailer) 
      forControlEvents:(UIControlEvents)UIControlEventTouchDown];
    [myImageView addSubview:button1];


-(void)goToFirstTrailer {
    [self startAnimator:@"OutsideToTrailer1_" forNumFrames:60];
}

The imageView this is placed on has buttons and User Interaction Enabled On.

Any light you can shed would be much appreciated.

Platitude answered 16/2, 2011 at 14:48 Comment(6)
Does the button highlight when you tap on it? Also, while this shouldn't cause non-firing, usually for buttons it's best to trigger the action on UIControlEventTouchUpInside.Clammy
Doesn't appear to highlight and that's the control even I've started with as well. No effect with that one either.Platitude
Are you subclassing UIImageView? If so, are you overriding any UIView or UIResponder methods related to events or hit testing?Mimicry
Edited: I am using UISwipeGestureRecogizers... and they don't work when I move them to myImageView either. myImageView is under UIView in IB. I haven't specifically over-rode anything that I'm aware of.Platitude
You might try setting cancelsTouchesInView to NO on the UISwipeGestureRecognizers. This is only supposed to prevent touch events when the gesture is successfully recognized so it shouldn't matter in this case, but it's worth trying.Clammy
Got it, checking "User Interaction Enabled" boxes in IB must not have been taking... setting it via code did the trick.Platitude
A
10

I think you have the wrong signature of the action method change that line to

-(void) goToFirstTrailer:(id)sender {

and where you set the action to

[button1 addTarget:self action:@selector(goToFirstTrailer:) forControlEvents:....

Important is the colon after the message name because I changed the action method signature to include the sender.

Edit I wrote a small sample project with just an imageView in the MainWindow.xib and created a button programmatically as follows

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button1.frame = CGRectMake(0.f, 0.f, 50.f, 50.f);
    [button1 setTitle:@"1" 
             forState:(UIControlState)UIControlStateNormal];
    [button1 addTarget:self
                action:@selector(goToFirstTrailer:) 
      forControlEvents:(UIControlEvents)UIControlEventTouchDown];
    imageView.userInteractionEnabled = YES; // <--- this has to be set to YES
    [imageView addSubview:button1];
    [self.window makeKeyAndVisible];

    return YES;
}

It is quick and dirty and yes, I am misusing the application delegate as the view controller. I know it is bad.

Here is the action method

- (void) goToFirstTrailer:(id)sender {
    imageView.backgroundColor = [UIColor redColor];
}

Setting the userInteractionEnabled property on the parent imageView makes the difference. With it set to NO which is the default, no events are routed to the button.

Alcazar answered 16/2, 2011 at 15:7 Comment(6)
No joy, just gave it a go. Button still isn't selecting nor is the event firing.Platitude
OK. what it also could be is that there is some view above the button. Could it be that there is a transparent view on top of it? Also check the property userInteractionEnabled on UIView if it is set to YES.Alcazar
Wait a minute! What about your imageView? Usually the imageView sets the userInteractionEnabled property to NO, because it just displays stuff. It could well be that this also means that it does not forward the events to its subviews. So try setting userInteractionEnabled to YES on the parent imageView.Alcazar
It was checked on both of those as well. UIImageView was the first thing I checked, I wish it was that easy.Platitude
Wrote an example project and tried it out without problems. Added the code to my answer.Alcazar
Oddly the checking "User Interaction Enabled" boxes in IB must not have been taking... setting it via code as you suggested did the trick.Platitude
T
0

If myImageView is a UIImageView object, adding a subview to it (like UIButton) only displays the image that object is represented by. If you add that UIButton as a subview of a UIView, it should work perfectly. Try adding an auxiliary UIView to your UIImageView and then add your button to a subview of the new auxiliary UIView, that should solve your problem.

Thomasinethomason answered 24/1, 2012 at 14:40 Comment(0)
C
0

This works fine for me. Note that I changed the event to UIControlEventTouchUpInside to allow the button press down state to be visible first.

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(20,400,88,35); //The position and size of the button (x,y,width,height)
[btn setTitle:@"Quit" forState:UIControlStateNormal];
[btn addTarget:self
            action:@selector(showAbout) 
  forControlEvents:(UIControlEvents)UIControlEventTouchUpInside];
[self.view addSubview:btn];
Court answered 14/7, 2012 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.