Reputation: 1
I'm trying to create a UIButton programmatically in iOS 14 (beta 3) in objective-C. This is what I've tried, but the UIAction handler is never called when I tap the button:
UIAction *tapAction = [UIAction actionWithHandler:^(UIAction* action){
NSLog(@"Never gets here");
}];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100) primaryAction:tapAction];
Any ideas?
Upvotes: 0
Views: 2693
Reputation: 145
I had the same issue. It looks like a bug to me. Try adding:
self.contentView.isUserInteractionEnabled = true
to init(style:reuseIdentifier:) if you are using a custom tableview cell. Please refer to https://developer.apple.com/forums/thread/661508?page=1#636261022 and @OOPer great answer.
Upvotes: 1
Reputation: 1
I wrote a test app that just displays a button and it's working, so the issue must be something else in my main app. Thanks all.
Upvotes: 0
Reputation: 1092
This is how I normally create a button in Objective-C:
- (void)createButton {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 50, 100);
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonClicked:(UIButton *)sender {
// Do your logic here
}
You can read more here: https://developer.apple.com/documentation/uikit/uicontrol/1618259-addtarget
Upvotes: 0