PDA

View Full Version : viewDidLoad getting called twice!!




usuf24
Sep 24, 2008, 01:38 AM
Hi,

I am traversing from my first view to the second view using the below mentioned code.


-(void)gotoNextView{
...
/*MainMenuController *mainMenuController = [[MainMenuController alloc]initWithNibName:@"MainMenu" bundle:[NSBundle mainBundle]];
self.menuController = mainMenuController;
[mainMenuController release];*/
self.menuController = [[MainMenuController alloc]initWithNibName:@"MainMenu" bundle:[NSBundle mainBundle]];
[self.view.window addSubview:[menuController view]];
...
...
}


In my second view however, its viewDidLoad method, where am initializing some of my variables, is being called TWICE.. Due to this all my variables and subviews of the second view are getting re-initialized.. When i use the touchesBegan event, the imageViews are becoming 0x0.. Can any1 please tell why this is happening and how to prevent this from happening..

Thanks n Regards,
USUF



usuf24
Oct 21, 2008, 08:48 AM
Hi,

Can anyone tell me in case of loading the view from the nib file, when does the viewDidLoad function of the viewController gets called....



Thanks and Regards,

Syed Yusuf

PhoneyDeveloper
Oct 21, 2008, 09:05 AM
Normally viewDidLoad is only called one time, after the nib is loaded. However, if a memory warning is sent then UIViewController releases its view and sets it to nil if the view controller isn't visible. The next time that the view appears the view controller will reload the view from the nib and call viewDidLoad again.

You have to assume that viewDidLoad can be called multiple times.

Implement didReceiveMemoryWarning and log or set a breakpoint to see what is happening.

indiekiduk
Mar 16, 2009, 12:36 PM
Normally viewDidLoad is only called one time, after the nib is loaded. However, if a memory warning is sent then UIViewController releases its view and sets it to nil if the view controller isn't visible. The next time that the view appears the view controller will reload the view from the nib and call viewDidLoad again.

You have to assume that viewDidLoad can be called multiple times.

Implement didReceiveMemoryWarning and log or set a breakpoint to see what is happening.

This is correct. If you need any other one-time initialization in the class the do it in -(void)awakeFromNib{}

johnnybluejeans
Mar 16, 2009, 02:27 PM
Any chance you are creating the view twice accidently?.. ie, gotoNextView is getting called twice?

Hi,

I am traversing from my first view to the second view using the below mentioned code.


-(void)gotoNextView{
...
/*MainMenuController *mainMenuController = [[MainMenuController alloc]initWithNibName:@"MainMenu" bundle:[NSBundle mainBundle]];
self.menuController = mainMenuController;
[mainMenuController release];*/
self.menuController = [[MainMenuController alloc]initWithNibName:@"MainMenu" bundle:[NSBundle mainBundle]];
[self.view.window addSubview:[menuController view]];
...
...
}


In my second view however, its viewDidLoad method, where am initializing some of my variables, is being called TWICE.. Due to this all my variables and subviews of the second view are getting re-initialized.. When i use the touchesBegan event, the imageViews are becoming 0x0.. Can any1 please tell why this is happening and how to prevent this from happening..

Thanks n Regards,
USUF