문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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.

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];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top