1

What is the Syntax for initializing instance variables and propertiesin the ViewController ? this is the code of the implementation

CalcViewController.m

#import "CalcViewController.h"

@interface CalcViewController ()
{
 int initialValue;
}

@property(nonatomic,weak) BOOl isFirstEntry;

@end


@implementation CalcViewController
@synthesize isFirstEntry = _isFirstEntry;
.......
.......
.......
.......
@end

-(id) init
{
self = [super init];
     if(self)
      {
      ............;
      }
return self;
}

this does not get triggered.

3
  • how are you initializing your view controller? is it though a nib file or programatically? Commented Mar 5, 2012 at 6:55
  • Where is this object being created? Are you actually calling init? Commented Mar 5, 2012 at 6:56
  • the project is a Single View Application and CalcViewController is the default ViewController Commented Mar 5, 2012 at 7:46

3 Answers 3

3

If you don't want to do it during a load method, then implement the designated initializer and initialize it there. If you do not redefine the designated initializer, then you inherit it from your superclass, and you would initialize in the superclass' designated initializer. UIViewController's designated initializer is:

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle

Sign up to request clarification or add additional context in comments.

Comments

1

The init method will be called when you create your object using

CalcViewController *calcView = [CalcViewController alloc] init]; or [CalcViewController new];

If you create the object using some other initiation method it wont be called. In a nutshell you will have to remember init is just like any other method.

Comments

0

i guess your ViewController is loaded from a nib.. (Xcode project template) try one of the following:

- (id)initWithCoder:(NSCoder)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self) {
        ...
    }
    return self;
}

But be aware that when this method is called all connections from Interface Builder are not set up (parent, children, actions, ...). If you want to rely on IB-connections you can use this method:

- (void)awakeFromNib{
    ...
}

Note that both methods will be called only when loading the ViewController itself from a NIB, not only its view. This is usually because XCode gives you a MainWindow.xib that contains a view controller inside (-> NIB).

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.