About push view controller into a navigation controller stack.

Discussion in 'iOS Programming' started by cthesky, Nov 8, 2012.

  1. cthesky macrumors member

    Joined:
    Aug 21, 2011
    #1
    Hi all,

    Below is two sample code of push view controller into a navigation stack. Anyone know which one is a more proper way to push view controller into a navigation controller and why it is. Any comments and suggestions are welcome. :)

    1.) Create a local variable for view controller and release it immediately after push it to navigation controller.

    Code:
    FirstViewController * firstVC = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
    [self.navigationController pushViewController:firstVC animated:NO];
    [COLOR="Blue"][firstVC release][/COLOR]
    
    * Should I release firstVC after push it in navigation controller? So far it is work fine for me but from some research I saw some people get their app crash when they release the view controller after push it and fixed if after remove the release message from code block. Am I doing in a right way?

    2.) Create an instance variable for a view controller, declare it in header and will be release in dealloc method.

    Code:
    FirstViewController * tempVC = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
    self.firstVC = tempVC;
    [tempVC release];
    [self.navigationController pushViewController:firstVC animated:NO];
    
    -(void)dealloc
    {
    [super dealloc];
    [self.firstVC release];
    }
    
    Is it a need to use instance variable? Actually when is a need to use instance variable?

    Thanks. :)
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    Hmm, apparently you're not completely into Obj-C Programming.
    Are you using ARC? You should learn the difference between property's & instance variables.
    (btw, the code will do exactly the same.. no matter what).
    First code block looks fine (if it's not ARC project).
    second block seems fine to, so it's just a matter of learning what the difference is, so I recommand studying a bit of Obj-C first, otherwise you're gonna get onto a mess if you keep coding random :)
     
  3. cthesky thread starter macrumors member

    Joined:
    Aug 21, 2011
    #3
    No, I am not using ARC.

    Yup, you are right. I am new to Objective-C programming and should study about it. Although sometimes I can get required output but I get confused and can't fully understand what happening in my code. Yup, I think I need study about it. Do you have any nice source to read for Objective-C programming? Any suggestions are appreciated.

    Thanks for your reply. :)
     
  4. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    There should be a sticky in the iPhone/iPad programming forum with alot of resources & help. I think it's really important for you to learn it, learn about memory management, but try to use ARC in your projects, it will help speed up your development without having to deal with memory management, but please, LEARN about it, so you know what it does, when to do it, so you have a general vision on it ^_-
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    The first example looks fine. Any crashes are due to bugs in the view controller being pushed.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    What do you know about instance variables and their scope?
     

Share This Page