Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cthesky

macrumors member
Original poster
Aug 21, 2011
91
0
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. :)
 
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 :)
 
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 :)

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. :)
 
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 ^_-
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.