简体   繁体   中英

UIButton selector not called no matter what I do

I know that this question was asked before many times but I didn't find the solution for my problem. I am not new at iOs and this should be pretty trivial stuff, but it is driving me mad and crazy :). Ok I have class C that is subclass of UIViewController. In it'S view there is a UIScrollView and in that scroll view I put class B and A also subclasses of UIViewController and I do it like this:

a = [[A alloc] initWithNibName:@"A" bundle:nil];
[a setDelegate:self];
[self addChildViewController:a];
[a.view setFrame:CGRectMake(2*vwMainScroller.frame.size.width, 0, a.view.frame.size.width, a.view.frame.size.height)];
[vwMainScroller addSubview:a.view];

b = [[B alloc] initWithNibName:@"B" bundle:nil];
[b setDelegate:self];
[self addChildViewController:b];
[b.view setFrame:CGRectMake(vwMainScroller.frame.size.width, 0, b.view.frame.size.width, speedScreen.view.frame.size.height)];
[vwMainScroller addSubview:b.view];

Both class A and class B have buttons on their views added programatically in viewDidLoad method like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *btnChangeColor = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 460)];
    [btnChangeColor addTarget:self action:@selector(Btn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnChangeColor];
}

-(void)Btn
{
    NSLog(@"I am the selector and I have been called");
}

But when I press class A button the selector is never called, but when I press class B button the selector is called just like it should be. I tried:

  • putting buttons on xibs
  • enabling user interaction for every view in my code - it was already enabled -> didn't help
  • painting buttons to see if they are where they should be -> they were
  • restarting computer and xCode
  • running on device and simulator

Nothing helped, always the same thing, button works on class B but not on class A, after I tried to add some other buttons in IB of class A, after I found out that they also don't call their selectors I checked the box "Shows touches on highlight" and guess what, they didn't glow when I touched them. So please I see that something is eating my touch events but just don't have any ideas how to proceed in debugging this matter. Please help me :)...

Try set up vwMainScroller.delaysContentTouches = Yes on your scroll view.

Add

[a.view setUserInteractionEnabled:YES];

and

[b.view setUserInteractionEnabled:YES]; 

before you add it as a subview to your scrollView, If it doesn't help set up your view controller as a delegate of your UIScrollView and add this:

-(BOOL)touchesShouldCancelInContentView:(UIView *)view
{
    return ![view isKindOfClass:[UIButton class]];
}

Ok, stop thinking, cause that is what U need to do to solve this problem. I stopped thinking and created a new class "Anew", copied all code from class A, created exactly the same xib, by copying elements from A.xib, deleted class A and implemented Anew in exactly the same way I did with class A. And now everything is working as it should. So the answer is when U spend 4 hours on trying to solve something as trivial as this, and u tried every possible approach that U can imagine and that others suggested, and nothing helps, it is time to stop thinking and start deleting cause the problem is probably deeper than U can reach...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM