简体   繁体   English

UIView:从自定义 XIB 加载导致崩溃

[英]UIView: Loading from custom XIB causing a crash

Hello StackOverflow.你好 StackOverflow。

  • I am trying to setup a UIView such that it loads itself from XIB file in Xcode.我正在尝试设置一个UIView ,以便它从 Xcode 中的XIB文件加载自己。

To do this, I went through the following initial steps:为此,我经历了以下初始步骤:

  1. Created empty UIView subclass.创建了空的UIView子类。
  2. Added a blank XIB file.添加了一个空白的XIB文件。

Then, I added all the subviews I wanted into the XIB file, and made corresponding IBOutlet Properties inside the UIView Subclass.然后,我将我想要的所有子视图添加到XIB文件中,并在UIView子类中制作相应的IBOutlet Properties

I watched this video to show me how to properly connect the outlets and set the files owner.我观看此视频是为了向我展示如何正确连接插座并设置文件所有者。 The video instructs me to do the following things:视频指导我做以下事情:

  • Do not set the UIView class to your subclass in XIB .不要将 UIView 类设置为XIB的子类。 Time link时间链接
  • Set the File's Owner to your UIView subclass in XIB : Time LinkFile's Owner设置为XIBUIView子类:时间链接
  • Insert a IBOutlet into your UIView class of type UIView so your XIB file can load into that.将 IBOutlet 插入到 UIView 类型的 UIView 类中,以便您的 XIB 文件可以加载到该类中。 Time link时间链接
  • Override initWithCoder like (image) if you intend to initialize the custom UIView within another XIB file.如果您打算在另一个XIB文件中初始化自定义UIView像 (image) 一样覆盖initWithCoder
  • Override initWithFrame like (image) if you intend to initialize the custom UIView programatically within another class file.如果您打算在另一个类文件中以编程方式初始化自定义UIView则覆盖initWithFrame like (image)

Cool, I have done all of these things, and am choosing to initialize my UIView programatically.酷,我已经完成了所有这些事情,并且选择以编程方式初始化我的UIView

See my UIView subclass implementation file :查看我的UIView子类实现文件

#import "CXHostsTableViewCellContentView.h"

@implementation CXHostsTableViewCellContentView



#pragma mark Custom Initializers

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [[NSBundle mainBundle]loadNibNamed:@"CXHostsTableViewCellContentView" owner:self options:nil];
        [self setBounds:self.view.bounds];
        [self addSubview:self.view];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [[NSBundle mainBundle]loadNibNamed:@"CXHostsTableViewCellContentView" owner:self options:nil];
        [self addSubview:self.view];
    }
    return self;
}

And of course, my header file :当然,我的头文件

#import <UIKit/UIKit.h>
#import "CXStyleView.h"

@interface CXHostsTableViewCellContentView : UIView

#pragma mark UIView Properties
@property (strong, nonatomic) IBOutlet UIView *view;

@property (nonatomic,weak)IBOutlet UIView *standardView;

@end

I also have an image here of the XIB file's owner and another of the IBOutlet link from the base UIView to the outlet on file's owner.这里还有一张 XIB 文件所有者的图像另一个从基础UIView到文件所有者的插座的IBOutlet链接。

Right, so everything's looking pretty good, should be no problem running this right?是的,所以一切看起来都不错,运行这个应该没问题吧?

Nope, whenever I initialize this subview and present it, I get a crash:不,每当我初始化这个子视图并呈现它时,我都会崩溃:

CXHostsTableViewCellContentView *scrollContentView = [[CXHostsTableViewCellContentView alloc]init];

异常截图

I've really got no idea how to solve this, as I'm sure I'm following all of these steps right.我真的不知道如何解决这个问题,因为我确定我正在正确地遵循所有这些步骤。 I've googled and come across this question which has the same symptoms, but the answer has identical code to what I'm using, and this question with a contradictory reply.我在谷歌上搜索并遇到了这个具有相同症状的问题,但答案与我使用的代码相同,并且这个问题的回答相互矛盾。

I'm not sure what to do at this point, or what is causing the crash.我不知道此时该做什么,或者是什么导致了崩溃。 I know that if I have NO outlets linked at all, it works.我知道,如果我根本没有连接任何网点,它就可以工作。 But then again, nothing displays either.但话又说回来,也没有任何显示。

I think that You will face Problem When You Allocate Memory to Your scrollContentView object.我认为当您为 scrollContentView 对象分配内存时会遇到问题。 so,Try To allocate Memory With Frame.所以,尝试用帧分配内存。

ie Write this in .m file在 .m 文件中写这个

- (void)myAllocation {
              
  //do your stuff
            
}
        
-  (id)initWithFrame:(CGRect)aRect {
           
   self = [super initWithFrame:aRect];
        
   if (self) {
      [self myAllocation];
   }
        
   return self;
        
}
        
 - (id)initWithCoder:(NSCoder*)aDecoder {
           
   self = [super initWithCoder:aDecoder];
   if (self) {
      [self myAllocation];
   }
        
   return self;
        
}

... ...

CXHostsTableViewCellContentView *scrollContentView = [[CXHostsTableViewCellContentView alloc] initWithFrame:CGRectMake(0, 10, 0, 20)];

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

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