【问题标题】:UIButton Click Not working when its triggered from static library当从静态库触发时,UIButton Click 不起作用
【发布时间】:2026-01-25 03:25:01
【问题描述】:

我们正在尝试使用 webview 从静态库项目中触发显示按钮。在将静态库与单视图应用程序集成时,我们得到了输出设计。但是当我们尝试通过单击按钮尝试操作时,它不起作用。

下面是我们在cocoa touch静态库中使用的代码

UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];

imageButton.frame = CGRectMake(self.view.frame.size.width-50, 10, 50, 50);

[imageButton setImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];

imageButton.adjustsImageWhenHighlighted = NO;

// [imageButton addTarget:self  action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];

[imageButton addTarget:self action:@selector(hideWebViewBtn:) forControlEvents:UIControlEventTouchUpInside];


-(IBAction)hideWebViewBtn:(id)sender {

      NSLog(@"Hide Button clicked");
     [self.adView removeFromSuperview];
     self.adView = nil;
     return;
 }

以下是集成并运行后在单视图应用程序中显示的屏幕截图。 一旦我点击按钮,它就不会被点击。

【问题讨论】:

  • 试试这个 imageButton.userInteractionEnabled =YES;以及您在哪里添加 imageButton?
  • 您是否在按钮顶部添加任何其他视图?

标签: ios objective-c xcode uibutton


【解决方案1】:

试试这个,不管self按钮是否触发..

  [self.myButton sendActionsForControlEvents:UIControlEventTouchUpInside];

它对我有用

【讨论】:

    【解决方案2】:

    您应该将您的按钮添加到超级视图。此外,超级视图应该启用用户界面。

    [yourSuperView addSubView:button];yourSuperView.userInteractionEnabled=YES;

    【讨论】:

      【解决方案3】:

      如何将“hideWebViewBtn”方法的返回类型从“IBAction”更改为“void”?

      【讨论】:

        【解决方案4】:

        “一旦我点击按钮,它就不会被点击。”你的意思是你点击了关闭按钮,但它没有调用函数 hideWebViewBtn:() ?

        【讨论】:

          【解决方案5】:

          你可以试试这个。

          UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-100, 10, 50, 50)];
          
              [imageButton setImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];
          
              [self.view addSubview:imageButton];
              [self.view bringSubviewToFront:imageButton];
              [imageButton removeTarget:self action:NULL forControlEvents:UIControlEventAllEvents];
          
              [imageButton addTarget:self action:@selector(hideWebViewBtn:) forControlEvents:UIControlEventTouchUpInside];
          

          希望它对你有用。谢谢!

          【讨论】:

            最近更新 更多