【问题标题】:UIView .xib buttons not workingUIView .xib 按钮不起作用
【发布时间】:2015-07-31 22:41:31
【问题描述】:

我创建了一个带有一些标签和一个按钮的 UIView .xib。我连接了按钮的所有插座和 IBAction。

当我从视图控制器添加 .xib 子视图时,它显示正常,但按钮不起作用。

这是我的 xib 的 .m 文件

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super initWithCoder:aDecoder])
    {
        [self load];
    }
    return self;
}


-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [self load];
    }
    return self;
}

-(void)load
{
    [[self superview] setUserInteractionEnabled:YES];
    [self setUserInteractionEnabled:YES];

    UIView *view = [[[NSBundle bundleForClass:[self class]]     loadNibNamed:@"AgregarAlCarrito" owner:self options:nil] firstObject];
    [self addSubview:view];
    view.frame = self.bounds;
}

我已经尝试了你能想象到的一切,但没有结果

谢谢

【问题讨论】:

  • 你的 .xib 有一个 UIViewController 还是只有一个 UIView ?
  • 我错误地创建了 .xib 对象,当我使用这个方法时它工作正常 NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"AgregarAlCarrito" owner:self options:nil]; UIView *mainView = [subviewArray objectAtIndex:0];

标签: ios objective-c uibutton xcode6 xib


【解决方案1】:

所以这条线在我看来很可疑:

[self addSubview:view];

您应该将 xib 视图添加到父视图。我在我的一个应用程序中做了这样的事情,我有这样的事情:

[[self view] addSubview:_xibView];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 2015-04-11
    • 2011-12-02
    相关资源
    最近更新 更多