iSanjay
iSanjay

Reputation: 173

Self.view calls viewDidLoad in iPhone

I am new to iPhone Development. Please Help me

I have a view controller calling method from NSObject class in the background for view controller when the method calls in that i am creating a view i wrote their self.view addSubview:view after this line my view did load calls again.

I dont know why this problem appears please help me here is my code.

NSObject.m

- (void) showModalMessage:(NSString *)mes
{
  self = [super init];
        if (self) {
            objViewController = [[ViewController alloc] init];
        }
    [objViewController showPopUp:mes];
}

ViewController.m
- (void) showPopUp:(NSString *)mes
{

    labelView = [[UIView alloc] initWithFrame:CGRectMake(470, 740, 380, 50)];
    [self setLabelViewSettings];
    label = [[UILabel alloc] initWithFrame:CGRectMake(20, 8, 340, 30)];
    [self setLabelSettings];
    [labelView addSubview:label];
    [label release];
    [self.view addSubview:labelView];// After This line View did load calls again 
    [labelView release];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.6];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(hide)];
    [UIView commitAnimations];
}

Sorry For my bad english please help me

Upvotes: 2

Views: 1989

Answers (1)

torrey.lyons
torrey.lyons

Reputation: 5589

Subclasses of UIViewController automatically call viewDidLoad whenever the view controller loads its view into memory. View controllers load their views only when needed. So in [self.view addSubview:labelView] the self.view is causing the view to be loaded into memory and viewDidLoad to be called. Immediately before this line the view property must have been nil and accessing the view property with self.view automatically loads the view into memory as described in the UIViewController Class Reference.

Note that viewDidLoad can be called multiple times because view controllers may unload their views and set their view property to nil in low memory situations. You need to make sure viewDidLoad is safe to call multiple times.

As jrturton pointed out you are setting self to a new object in showModalMessage:, which is wrong. This guarantees that when you get to showPopUp: your newly created ViewController object will not have loaded its view yet so you will always call viewDidLoad when you hit self.view.

Upvotes: 4

Related Questions