• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在另一个 XIB 中指定类时如何使用 Custom View.xib 加载自定义 View ?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 08:52:11 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我使用 CustomView.xib 文件创建 CustomView:UIView。 现在我想通过将 View 拖到另一个 XIB(例如:UIViewController.xib)并选择类:customView 来使用它。

我初始化CustomView并将SubView添加到另一个 View 时可以加载成功:

- (void)viewDidLoad{
    //Success load NIB
    CustomView *aView = [[CustomView alloc] initWithFrame:CGRectMake(40, 250, 100, 100)];
    [self.view addSubview:aView];
}

//自定义 View .m

- (id)initWithFrameCGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSLog(@"INIT");
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed"CustomCell" owner:self options:nil];
        [[nib objectAtIndex:0] setFrame:frame];
        self = [nib objectAtIndex:0];
    }
    return self;
}

在这种情况下,通过将 CustomCell 绘制到另一个 XIB 中来重用它,然后将类指定为 CustomView。我知道 awakeFromNib 被调用,但不知道如何加载 CustomView.xib。 该怎么做?

*编辑:

initWithCoder 在指定类时也会被调用,但它会创建一个循环并与 loadNibNamed 一起崩溃。为什么会这样?

- (id)initWithCoderNSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        NSLog(@"Coder");
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed"QSKey" owner:nil options:nil];
        [[nib objectAtIndex:0] setFrame:self.bounds];
        self = [nib objectAtIndex:0];
    }
    return self;
}



Best Answer-推荐答案


在您的 View Controller 中直接拖动自定义 xib 是不可能的。但是您可以做一件事,拖动其父类(super class) View 并将其类设置为您的自定义类。并将其属性连接到您将根据您的自定义放置的对象类。

您可以使用以下代码直接加载您的自定义 xib:

//使用代码加载xib ....

   QSKey *keyView=[[QSKey alloc] init];
   NSArray *topObjects=[[NSBundle mainBundle] loadNibNamed"QSKey" owner:self options:nil];

for (id view in topObjects) {
    if ([view isKindOfClass:[QSKey class]]) {
        keyView=(QSKey*)view;
    }
}

keyView.label.text=@"CView";
[keyView setFrame:CGRectMake(0, 0, 100, 100)];
[keyView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:keyView];

here label is UILabel object you have used in your custom class as its property.


//Load custom View using drag down objects of type Custom Class Super class.

for (QSKey *aView in self.view.subviews) {
    if ([aView isKindOfClass:[QSKey class]]) {
        [self addGestureRecognizersToPiece:aView];
        aView.label.text=@"whatever!";
    }
}

Here label is object of UILabel you have to place on your dragged View to view controller's xib view.Change that view's class to your Custom Class.Then it will will show its label property in outlet connect it to your UILabel object placed over dragged view.

这是一个工作代码 TestTouchonView

屏幕截图将显示如下。

enter image description here

关于ios - 在另一个 XIB 中指定类时如何使用 Custom View.xib 加载自定义 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14954523/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap