viewDidLoad won't fire the second time view is loaded - HELP

Discussion in 'iOS Programming' started by ddaddy, Jun 30, 2011.

  1. ddaddy macrumors newbie

    Joined:
    Jun 30, 2011
    #1
    Hi,
    I have 2 views that switch between each other. The first time they are loaded, the viewDidLoad functions are called, however as the views are switched back and forth, they viewDidLoad is never called again.
    Neither Dealloc nor viewDidUnload are called at any point.

    The obvious answer is that I haven't deallocated something, however 1 of the views has just 1 button on it (to switch the views) and this is all the code it has

    Code:
    -(IBAction)toView2:(id)sender
    {
    	Slider2AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    	[appDelegate switchToView2:self.view];
    	appDelegate = nil;
    	[Slider2AppDelegate release];
    	[appDelegate release];
    }
    Any idea whats going wrong here?

    Thanks
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Code:
    [Slider2AppDelegate release];
    
    This is sending release to a class. You don't want to do this.

    Code:
    	[appDelegate release];
    
    This is doing nothing because two lines about you set appDelegate to nil. And if it wasn't nil you don't want to do this. This method hasn't retained the appDelegate and hasn't obtained the appDelegate in a context that requires it to send release. See the memory management guide for details on when you should and shouldn't send retain and release.

    The meat of the view switching functionality seem to be in your app delegate's switchToView2. Post that code.
     
  3. CalWizrd Suspended

    CalWizrd

    Joined:
    Jun 21, 2011
    Location:
    NYC/Raleigh, NC
    #3
    In addition to the comments posted previously... a view doesn't get loaded every time it appears. You should consider using viewWillAppear instead of viewDidLoad.
     
  4. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    The guy should set his controller pointer to release, this is the most common way of Apple.
    (look at basic navigation project --> then didSelectRowAtIndexPath.)

    But setting your appdelegate to nil is enough, don't need another release, Sydney was right about that (I think.)

    Then to be sure, I will tell you that the guy above me is right.
    You are loading the view Onto the stack, so it will remain on there (with a navigationController.) So the viewDidload only gets triggered once.
    To call the things u want, u need to put it into the
    Code:
    - (void) viewWillAppear:(bool)animated {
     [super viewWillAppear:animated];
    }
    That should do it ;)
     
  5. ddaddy thread starter macrumors newbie

    Joined:
    Jun 30, 2011
    #5
    Thanks for the replies guys.

    This is the switchToView2 code
    Code:
    -(void)switchToView2:(UIView *)fromView
    {
    	[fromView removeFromSuperview];
    	[window addSubview:firstGame.view];
    	NSLog(@"view1 count = %i view2 count = %i",[fromView retainCount],[firstGame.view retainCount]);
    	fromView = nil;
    }
    Between you, you have explained a lot more to me than my book did!
    So the view always stays loaded, so viewDidLoad only fires ones, but I can use viewWillAppear to reset my views.

    I've followed several tutorials for switching views, and they all did it a different way. One way did infact fire viewDidLoad each time it switched, but the memory crept up each time the views where switched as if it was retaining something each time.
     
  6. Sykte, Jul 1, 2011
    Last edited: Jul 1, 2011

    Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #6
    Why do you think he should release or nil out this object? Do you think he owns the object?
     
  7. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #7
    Ddaddy I would recommend reading over the memory management rules. From your code you do not have a good grasp on how it works. http://developer.apple.com/library/...a/Conceptual/MemoryMgmt/Articles/mmRules.html
     
  8. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #8
    Oops, I think i miswatched. Cuz that's an autoreleased appdelegate no?!
    Then I told him wrong. and I thought he did a navigationContorller instead of just adding a subView. that's what I mixed it up ;)
    because at the end of a navigationpush u have to release your alloc/inited pointer.
     

Share This Page