加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 如何在Interface Builder中使用自定义UIButton?

发布时间:2020-12-14 17:56:45 所属栏目:百科 来源:网络整理
导读:我有一个子类UIButton加载一个Nib: @implementation BlaButtonVC- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { NSArray *subViews = [[NSBundle mainBundle] loadNibNamed:@"BlaButton" owner:self options:nil]; [
我有一个子类UIButton加载一个Nib:

@implementation BlaButtonVC

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSArray *subViews = [[NSBundle mainBundle] loadNibNamed:@"BlaButton" owner:self options:nil];
        [self addSubview:subViews[0]];
    }
    return self;
}

@end

我可以将此按钮添加到视图中,如下所示:

// ViewController.m

BlaButtonVC *button = [[BlaButtonVC alloc] initWithFrame:CGRectMake(10,10,280,44)];

button.titleXLabel.text = @"Nyuff";
button.descLabel.text = @"Kukk";

[self.view addSubview:button];

我的问题是我不知道如何从Interface Bulder这样做,如何使用这个UIButton子类而不是正常的子类.

已将Button Type更改为Custom,将Custom Class更改为BlaButtonVC,但这还不够.它将调用BlaButtonVC initWithFrame,但按钮不会出现在Interface Builder中,也不会出现在Simulator中.

我错过了什么?

在这里您可以找到完整的样本:
https://www.dropbox.com/s/ioucpb6jn6nr0hs/blabla1.zip

解决方法

从StoryBoard初始化时,它调用方法initWithCoder:而不是initWithFrame:.除此之外,一切都是正确的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读