2

I'm developing an iOS app with latest SDK.

I have created a class that inherits from UIView and I have to do some initialization every time the class is instantiated.

I have to call a method called setUpVars: but I don't know where to send a message to that method:

- (id)initWithFrame:(CGRect)frame;
- (id)initWithCoder:(NSCoder*)aDecoder;

This class can be used with a custom xib, or added to a Storyboard, so I need to be sure that that method will be called on every case.

- (void)setUpVars
{
    _preferenceKey = @"";
    _preferenceStatus = NO;
    _isDown = NO;
}

Where do I have to add [self setUpVars];?

2
  • can you show the setUpVars method?
    – pdrcabrod
    Mar 12, 2013 at 9:15
  • @pdrcabrod Question updated.
    – VansFannel
    Mar 12, 2013 at 9:16

5 Answers 5

2

Essentially you will be wanting to cover both cases

- (id)initWithFrame:(CGRect)frame;
{
  self = [super initWithFrame:frame];
  if (self) {
    [self setUpVars];
  }
  return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder;
{
  self = [super initWithCoder:aDecoder];
  if (self) {
    [self setUpVars];
  }
  return self;
}
2
  • Just don't forget init:. Murphy said it can be called therefore it will be called. Mar 12, 2013 at 10:30
  • @HermannKlecker as initWithFrame: is the designated initializer init will generally be overridden to call through to it, which is what a quick test showed.
    – Paul.s
    Mar 12, 2013 at 10:38
0

I think that you need to send this message from each method, also do not forget about awakeFromNib method.

You can create BOOL variable, something like isAlreadySetup and set it to YES in setUpVars method.

0
0

Docs Says

awakeFromNib

Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file.

- (void)awakeFromNib
{
 [self setUpVars];
}
0

If you use Interface Builder to design your interface, initWithFrame: is not called when your view objects are subsequently loaded from the nib file. Instead initWithCoder gets called. So you can initialize your variables in both methods if you prefer a generic way. Works in both case

-1

I tend to think you should call this method from the -(void)viewDidLoad method of the controller in charge

3
  • the viewDidLoad is the correct place to do work on subviews because you know than when it is called all views defined by interface builder haven been correctly loaded. Furthermore, variable management is to be done on a controller not on a view. Mar 12, 2013 at 9:40
  • Those variables correctly or incorrectly are the view's internal state not the viewControllers, it does not make sense for the viewController to set up defaults for the view's state and would make for a very fragile design
    – Paul.s
    Mar 12, 2013 at 9:51
  • Initialization should be done in the init methods of the object itself. The UIView subclass should not depend on external classes, including owners, to initialize it properly. viewDidLoad is a perfect place to set any of the properties (if they are properties) different from the initialization defaults. Mar 12, 2013 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.