简体   繁体   中英

UIView: Loading from custom XIB causing a crash

Hello StackOverflow.

  • I am trying to setup a UIView such that it loads itself from XIB file in Xcode.

To do this, I went through the following initial steps:

  1. Created empty UIView subclass.
  2. Added a blank XIB file.

Then, I added all the subviews I wanted into the XIB file, and made corresponding IBOutlet Properties inside the UIView Subclass.

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 . Time link
  • Set the File's Owner to your UIView subclass in XIB : Time Link
  • Insert a IBOutlet into your UIView class of type UIView so your XIB file can load into that. Time link
  • Override initWithCoder like (image) if you intend to initialize the custom UIView within another XIB file.
  • Override initWithFrame like (image) if you intend to initialize the custom UIView programatically within another class file.

Cool, I have done all of these things, and am choosing to initialize my UIView programatically.

See my UIView subclass implementation file :

#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.

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. so,Try To allocate Memory With Frame.

ie Write this in .m file

- (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)];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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