Infinite loop when overriding initWithCoder

前端 未结 5 842
臣服心动
臣服心动 2020-12-28 08:54

I have a UIViewController with some controllers and some views. Two of these views (Grid Cell) are other nibs. I\'ve got outlets from the Grid Cells to File\'s

相关标签:
5条回答
  • 2020-12-28 09:20

    Loading the nib causes initWithCoder to be called again, so you only want to do so if the subclass currently doesn't have any subviews.

    -(id)initWithCoder:(NSCoder *)aDecoder {
        self = [super initWithCoder:aDecoder];
        if (self) {
            if (self.subviews.count == 0) {
                UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
                UIView *subview = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
                subview.frame = self.bounds;
                [self addSubview:subview];
            }
        }
        return self;
    }
    
    0 讨论(0)
  • 2020-12-28 09:28

    loadNibNamed:: will call initWithCoder:

    Why don't you follow this pattern?

    -(id)initWithCoder:(NSCoder *)coder
    {
    
        if (self = [super initWithcoder:coder]) {
    
           // do stuff here ... 
    
        }
    
        return self;                 
    }
    

    Does [super initWithcoder:coder] do things that you want to avoid?

    0 讨论(0)
  • 2020-12-28 09:31

    Loading a nib will cause the corresponding owner in a

      -(id) initWithCoder:(NSCoder *) coder;  
    

    call

    Therefore your coude in this method:

    self = [[[NSBundle mainBundle] loadNibNamed: @"GridCell"
    owner: self
    options: nil] objectAtIndex:0];
    

    will cause again a call of the initWithCoder method. That's because you try to load the nib again. If you define a custom UIView and create a nib file to lay out its subviews you can't just add a UIView to another nib file, change the class name in IB to your custom class and expect the nib loading system to figure it out. What you could do is the following:

    Your custom view's nib file needs to have the 'File's owner' class set to your custom view class and you need to have an outlet in your custom class called 'toplevelSubView' connected to a view in your custom view nib file that is acting as a container for all the subviews. Add additional outlets to your view class and connect up the subviews to 'File's owner' (your custom UIView). (See https://stackoverflow.com/a/7589220/925622)

    EDIT Okay, to answer your edited question I would do the following:

    Go to the nib file where you want to include the custom view with it's nib file layouting it. Do not make it to the custom view (GridCell) itself, instead make a view which will contain your grid cell (gridCellContainer for example, but it should be a UIView) Customize the initWithFrame method within your custom view like you did in initWithCoder:

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"GridCell" owner:self options:nil];    
            self = [nib objectAtIndex:0];
            self.frame = frame;
        }
        return self;
    }
    

    And then, in the viewController which is the fileOwner for the view where you want to include your custom view (the one with the gridCellContainer view) do this in viewDidLoad e.g.

    //...
    GridCell *gridCell = [[GridCell alloc] initWithFrame:self.viewGridCellContainer.bounds];
    [self.viewGridCellContainer addSubview:gridCell];
    

    Now eveything should work as you expected

    0 讨论(0)
  • 2020-12-28 09:37

    The File's owner will not get a call to

      -(id) initWithCoder:(NSCoder *) coder;  
    

    when loading a xib.

    However, every view defined in that xib will get a call to

    -(id) initWithCoder:(NSCoder *) coder;  
    

    when loading a xib.

    If you have a subclass of a UIView (i.e. GridCell) defined in a xib and also try to load that same xib in your subclass's initWithCoder, you will end up with an infinite loop. However, I can't see what will the use case be.

    Usually you design your UIView's subclass (i.e. GridCell) in one xib, then use that subclass in a view controller’s xib for example.

    Also, can't see a use case where your custom view will have a subview in it's initWithCoder, i.e.

    -(id)initWithCoder:(NSCoder *)aDecoder {
        self = [super initWithCoder:aDecoder];
        if (self) {
            if (self.subviews.count == 0) {
                UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
                UIView *subview = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
                subview.frame = self.bounds;
                [self addSubview:subview];
            }
        }
        return self;
    }
    

    Unless you want to be able to override its view hierarchy on demand in some other xib. Which IMO assumes an external dependency (i.e. a hierarchy defined in another xib) and kinda defeats the purpose of having a reusable UIView in the first place.

    Bare in mind that when loading a xib, passing an instance as the File's owner, will have all of its IBOutlet(s) set. In that case, you would be replacing self (i.e. GridCell) with whatever the root view in that GridCell.xib is, losing all IBOutlet connections in the process.

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"GridCell" owner:self options:nil];    
            self = [nib objectAtIndex:0];
            self.frame = frame;
        }
        return self;
    }
    

    There is a more detailed post on "How to implement a reusable UIView." that goes into a bit more detail as well and hopefully clears things up.

    0 讨论(0)
  • 2020-12-28 09:43

    I had the same issue when I'm trying to override initWithsomething method, we need

    -(id)initWithsomething:(Something *)something
    {
        if (self = [super initWithsomething:something]) {
           // do stuff here ... 
        }
    
        return self; 
    }
    

    instead

    -(id)initWithsomething:(Something *)something
    {
        if (self = [super init]) {
           // do stuff here ... 
        }
    
        return self;
    }
    
    0 讨论(0)
提交回复
热议问题