简体   繁体   English

iOS视图上的自定义子视图具有空出口

[英]Custom Subview on a iOS view has null outlets

I have a view called FeedView, handled by FeedViewController. 我有一个名为FeedView的视图,由FeedViewController处理。

I also have a XIB called "NearestStore" which is handled by a view controller named "NearestStoreViewController". 我还有一个名为“NearestStore”的XIB,它由名为“NearestStoreViewController”的视图控制器处理。 NearestStore xib has labels, buttons, etc. In the view controller I have outlets that are connected to the subviews in NearestStore.xib. NearestStore xib有标签,按钮等。在视图控制器中,我有连接到NearestStore.xib中子视图的插座。

NearestStore inherits from UIButton (so it's easier to handle click event). NearestStore继承自UIButton(因此处理click事件更容易)。

On FeedViewController.xib I have a UIButton that has been set to be of type NearestStore. 在FeedViewController.xib上,我有一个UIButton,它已被设置为NearestStore类型。

So far so good. 到现在为止还挺好。 This is on my FeedViewController: 这是在我的FeedViewController上:

__weak IBOutlet NearestStoreButton *btn_nearestStore;

The outlet is connected on the xib to the outlet. 插座在xib上连接到插座。

NearestStoreViewController has several outlets to subviews like: NearestStoreViewController有几个子视图出口,如:

@property (nonatomic, weak) IBOutlet  UILabel *lbl_distance; 
@property (nonatomic, weak) IBOutlet  UIImageView *img_distance;

For some reason, on my FeedViewController the reference to btn_nearestStore is fine, but all the subviews are nil. 出于某种原因,在我的FeedViewController上,对btn_nearestStore的引用很好,但所有子视图都是nil。

For example: 例如:

btn_nearestStore.lbl_distance 

is nil 没有

What am I missing? 我错过了什么?

This sounds exactly as the system is supposed to work. 这听起来就像系统应该工作一样。 It is not easy to create a custom widget using xibs. 使用xib创建自定义小部件并不容易。

Here's how it works: 以下是它的工作原理:

Your FeedViewController will preform xib loading for the corresponding FeedView. 您的FeedViewController将为相应的FeedView执行xib加载。 During this load, it notices the NearestStoreButton subview. 在此加载过程中,它会注意到NearestStoreButton子视图。 As a consequence, it creates such a view using the - (id)initWithCoder: message on the NearestStoreButton class. 因此,它使用NearestStoreButton类上的- (id)initWithCoder:消息创建这样的视图。 It will not magically notice the corresponding .xib nor the corresponding viewController. 它不会神奇地注意到相应的.xib和相应的viewController。

If you need to use a xib within a xib, you need to do the loading manually for all subviews. 如果需要在xib中使用xib,则需要手动为所有子视图加载。 Keep in mind that you somehow need to create/use the appropriate owners (view controllers) for these secondary xibs. 请记住,您需要为这些辅助xib创建/使用适当的所有者(视图控制器)。

It's hard to tell from your description, but this sounds like a problem with "owner" of the loaded NearestStoreButton XIB. 从你的描述中很难说,但这听起来像是加载的NearestStoreButton XIB的“所有者”的问题。 When you load a NIB, you give the loader an owner, and its this owner on which most outlet bindings and actions are made. 加载NIB时,您将为加载程序提供一个所有者,并为其所有者提供大多数出口绑定和操作。 If you're loading your NearestStoreButton with UINib , then when you call instantiateWithOwner:options: , make sure you pass the object on which the outlets should be set as the owner. 如果你加载你的NearestStoreButtonUINib ,那么当你调用instantiateWithOwner:options: ,请确保您传递其出口应设置为所有者的对象。

When are you calling the outlet? 你什么时候打电话给插座? If you are trying to access the property in the initWithCoder method of the view, it's not guaranteed that object has been instantiated. 如果您尝试在视图的initWithCoder方法中访问该属性,则无法保证该对象已实例化。

If you access your property in the awakeFromNib method within the view you should be able to get it. 如果您在视图中的awakeFromNib方法中访问您的属性,您应该能够获取它。 For instance, I have a custom view, and my code looks as such: 例如,我有一个自定义视图,我的代码看起来像这样:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        //Don't style the subviews in here since they won't be initialized
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self styleViews];
}

- (void)styleViews
{
     //access my properties and style them in here
}

Following post contains detailed explanation about creating custom views using Nib: 以下帖子包含有关使用Nib创建自定义视图的详细说明:

creating custom view using Nib 使用Nib创建自定义视图

After I create the customInit as mentioned in the post, I am able to get the IBOutlets allocated. 在我创建帖子中提到的customInit后,我能够分配IBOutlets。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM