viewDidLoad getting called twice!!

Discussion in 'iOS Programming' started by usuf24, Sep 23, 2008.

  1. usuf24 macrumors newbie

    Aug 21, 2008

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

    /*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,
  2. usuf24 thread starter macrumors newbie

    Aug 21, 2008
    When does the viewDidLoad gets called!!!!!


    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
  3. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    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.
  4. indiekiduk macrumors 6502

    Jul 26, 2005
    Glasgow, Scotland
    This is correct. If you need any other one-time initialization in the class the do it in -(void)awakeFromNib{}
  5. johnnybluejeans macrumors 6502

    Jan 16, 2006
    New York, NY
    Any chance you are creating the view twice accidently?.. ie, gotoNextView is getting called twice?


Share This Page