0

我正在尝试在 Objective-C 的 iOS 14(beta 3)中以编程方式创建 UIButton。这是我尝试过的,但是当我点击按钮时 UIAction 处理程序永远不会被调用:

UIAction *tapAction = [UIAction actionWithHandler:^(UIAction* action){
     NSLog(@"Never gets here");
}];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100) primaryAction:tapAction];

有任何想法吗?

4

3 回答 3

1

我遇到过同样的问题。对我来说它看起来像一个错误。尝试添加:

 self.contentView.isUserInteractionEnabled = true

如果您使用的是自定义表格视图单元格,则为init (style:reuseIdentifier:) 。请参阅https://developer.apple.com/forums/thread/661508?page=1#636261022和 @OOPer 很好的答案。

于 2020-09-25T06:35:52.587 回答
0

这就是我通常在 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
}

您可以在这里阅读更多内容: https ://developer.apple.com/documentation/uikit/uicontrol/1618259-addtarget

于 2020-08-05T07:29:33.920 回答
0

我编写了一个测试应用程序,它只显示一个按钮并且它正在工作,所以问题一定是我的主应用程序中的其他问题。谢谢大家。

于 2020-08-05T23:33:33.453 回答