【问题标题】:Custom UIButton that generates UIView in SuperView在 SuperView 中生成 UIView 的自定义 UIButton
【发布时间】:2015-01-23 03:31:00
【问题描述】:

我正在试验一个自定义类按钮,在触摸启动时显示 UIView。 我有一个 UIButton 类名为 TapInButton。这是代码。

#import "TapInButton.h"
#import "TapInViewController.h"

@interface TapInButton()
@property (nonatomic,strong) TapInViewController * tapIn;
@end
@implementation TapInButton
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    self.tapIn = [[TapInViewController alloc] initWithNibName:@"TapIn" bundle:nil];
    self.tapIn.view.frame = CGRectMake(0, 50, 300, 500);
    self.tapIn.view.backgroundColor = [UIColor redColor];
    
    [self.superview addSubview:self.tapIn.view];

    
}

如您所见,我导入生成 xib 文件的 TapInViewController。 当用户单击具有自定义类 TapInButton 的 Tapin 按钮时。 它显示 xib 文件。见下图

我成功地将 xib 添加到主 ViewController 但问题是我无法单击 xib 文件中的按钮。我的代码中是否缺少某些内容?或者这是否可以在自定义 UIButton 中生成 uiview?

我知道生成 xib 文件但不使用自定义按钮的另一种方法。如果可能的话,我只想澄清一下我的想法。希望得到您的建议或解释。提前致谢

【问题讨论】:

  • 你有 userInteractionEnabled 吗?
  • 我也不确定这是否完全必要,但您通常会调用 [super touchesBegan:touches withEvent: event];在方法的开头。
  • @JoshGafni 抱歉回复晚了。我只是猜测这是否有效。我是这个自定义课程的新手。我尝试启用 userInteractionEnable 但输出相同。我无法单击我的 xib 文件中的按钮。

标签: ios uiview uiviewcontroller uibutton


【解决方案1】:

问题是我没有点击 xib 文件中的按钮。我的代码中是否缺少某些内容?

您必须在代码中获取对按钮的引用并在代码中设置按钮单击操作连接(通过调用addTarget:action:forControlEvents:)。

【讨论】:

  • 抱歉回复晚了。我在 TapInViewController 中尝试并在 viewdidload 中添加 addTarget:action:forControlEvents: 但它不起作用。
猜你喜欢
  • 2013-04-09
  • 2019-08-09
  • 2011-07-03
  • 2019-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多