What is the correct way to perform initialization, regardless of whether the object is loaded from the tip or created programmatically?

I noticed that if you load connected views from nib, you need to override initWithCoder, if you want to add the initialization code, because the designated initializer is not called (which makes sense), and if you do not load the view from the tip, then the same code should be executed in the designated initializer.

So, in order to handle both cases, you need the same initialization code in both methods.

This is the best solution I've come up with so far, but I need to think about whether there is an even more common way to do this. This code is in a subclass of UITableViewCell, but it can be any UIView:

/* * Seems like there should be a standard method for this already. */ - (void)didFinishInitializingOrUnacrhiving { /*** Do stuff that makes the most sense to do in an initializer. ***/ } - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { [self didFinishInitializingOrUnacrhiving]; } return self; } - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { [self didFinishInitializingOrUnacrhiving]; } return self; } 

So, any thoughts on this? Is this the โ€œright wayโ€ to do something, are there potential traps here, or am I just missing something?

+4
source share
2 answers

I do the same thing except that I'm lazy and my method is usually called -didInit .

+1
source

Source: https://habr.com/ru/post/1299272/


All Articles