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

Posted

技术标签:

【中文标题】在另一个 XIB 中指定类时如何使用 Custom View.xib 加载自定义视图?【英文标题】:How to load CustomView with CustomView.xib when specify class in another XIB? 【发布时间】:2013-02-19 09:51:38 【问题描述】:

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

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

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

//自定义视图.m

- (id)initWithFrame:(CGRect)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)initWithCoder:(NSCoder *)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;

【问题讨论】:

没有得到你..你到底想要什么?? 我想将CustomView与其xib一起加载但不使用initWithFrame,只需将视图拖到另一个Xib然后指定类。但是XIB不加载。 我想这就是你想要做的,不是吗? ***.com/a/5096476/840428 @followben 谢谢!和我的问题一样。但是我在 initWithCoder 中做了同样的事情,它创建了一个循环,我的应用程序崩溃了!仍在调查! 那是因为您仍在尝试将 self 设置为在 nib 中实例化的对象,这将反过来实例化一个新实例,该实例将尝试将 self 设置为在最后一个 nib 中实例化的对象。 ..等等无限循环。在我链接到的答案中,请注意他正在拉出对象并将其设置为子视图,而不是将其设置为 self。 【参考方案1】:

在您的视图控制器中直接拖动自定义 xib 是不可能的。但是您可以做一件事,拖动其超类视图并将其类设置为您的自定义类。并将其属性连接到您将根据您的自定义放置的对象类。

您可以使用以下代码直接加载您的自定义 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

屏幕截图将显示如下。

【讨论】:

以上是关于在另一个 XIB 中指定类时如何使用 Custom View.xib 加载自定义视图?的主要内容,如果未能解决你的问题,请参考以下文章

在linux中指定类路径* .jar时无法添加多个jar [重复]

如何在另一个视图中加载 xib 文件?

如何在 XIB 中指定左、右或标题视图按钮

如何在另一个 ViewController 中将带有 Xib 的 ViewController 显示为 SubView

在另一个 xib 中使用 xib 对象

xcode - 将 xib 文件附加到自定义 UIView 类时出错