1

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

Screenshot of exception

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.

Community
  • 1
  • 1
Micrified
  • 3,338
  • 4
  • 33
  • 59
  • You probably need to continue the execution after the crash and give us what you see in the console (stack trace, exception details, etc.) – mohamede1945 Sep 12 '15 at 13:10
  • @mohamede1945 There is nothing but some scant information concerning memory addresses. No telling names, or figures stand out at all. – Micrified Sep 12 '15 at 17:45
  • Hi @Owatch, did you fix this? I'm currently experiencing this. – KarenAnne Jan 05 '17 at 07:26

1 Answers1

0

I think that You will face Problem When You Allocate Memory to Your scrollContentView object. so,Try To allocate Memory With Frame.

i.e 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)];
Avineet Gupta
  • 586
  • 10
  • 21
Bhumesh Purohit
  • 491
  • 1
  • 8
  • 26
  • I don't understand your response, what is myAllocation? How does this solve my problem? – Micrified Sep 12 '15 at 17:54
  • Hello Owatch, Can you send me sample/demo project for this? So, that I can help you. – Bhumesh Purohit Sep 14 '15 at 08:54
  • I'll post it here in 2 hours. – Micrified Sep 14 '15 at 08:57
  • https://drive.google.com/file/d/0BwYE5uc9NjE-dlNFTGpSUU5WcGM/view Here is the link, accidentally gave it to someone else who commented earlier. My bad. To simulate the crash, simply go open up the App and try accessing the "Hosts" screen off the menu. – Micrified Sep 14 '15 at 13:36