views:

37

answers:

1

In objective-c (cocoa touch) I have a series of UIViewControllers that I am switching between.

- (void)switchViews:(id)sender
{
    UIButton *button = (UIButton *)sender;
    UIViewController *nextViewController;
    int tag = button.tag;

    switch (tag)
    {
        // -- has already been created
        case kFinancialButton:
            nextViewController = financials;
            break;

        case kSocialButton:
            if (social == nil)
            {
                SocialViewController *socialViewController = [[SocialViewController alloc] initWithNibName:@"SocialViewController" bundle:nil];
                social = socialViewController;
                [socialViewController release];
            }
            nextViewController = social;
            break;

        case kTicketingButton:
            if (ticketing == nil)
            { 
                TicketingViewController *ticketingViewController = [[TicketingViewController alloc] initWithNibName:@"TicketingViewController" bundle:nil];
                ticketing = ticketingViewController;
                [ticketingViewController release];
            }
            nextViewController = ticketing;
            break;
    }

        ///////
------> // -- [button/nextViewController release]????
        ///////

    [self setActiveButton:button];
}

As you can see, I'm assigning one of the view controllers to "nextViewController". What I'm wondering is if I need to release this "local" variable or if it's ok to leave alone since it's just pointing to one of my view controllers (which I release in dealloc). I don't think "tag" needs to be released since it's a "primitive", correct? How about button? I don't quite understand what should and shouldn't be released explicitly so perhaps I'm being overcautious. Thanks in advance.

+1  A: 

In general you only have to release a variable that you've retain'd init'd or copy'd.

Edit:

After reading your code a little bit more, it seems like you'd have other issues with bad values. The below code makes a little more sense to me. This assumes that financials, social, and ticketing are all @synthesized ivars.

- (void)switchViews:(id)sender
{
    UIButton *button = (UIButton *)sender;
    UIViewController *nextViewController;
    int tag = button.tag;

    switch (tag)
    {
        // -- has already been created
        case kFinancialButton:
            nextViewController = self.financials;
            break;

        case kSocialButton:
            if (!social) {
                self.social = [[[SocialViewController alloc] initWithNibName:@"SocialViewController" bundle:nil] autorelease];
            }
            nextViewController = self.social;
            break;

        case kTicketingButton:
            if (!ticketing) {
                self.ticketing = [[[TicketingViewController alloc] initWithNibName:@"TicketingViewController" bundle:nil] autorelease];
            }
            nextViewController = self.ticketing;
            break;
    }

    // Do something with nextViewController I'd assume

    [self setActiveButton:button];
}
Bryan McLemore
Yeah, it ended up looking like this: social = [[SocialViewController alloc] initWithNibName:@"SocialViewController" bundle:nil];
Typeoneerror
I don't think I need the "self." do I? Totally assuming that the scope defaults to "self." here. Also, I didn't use autorelease, since I retain and release then explicitly in this method and dealloc. Is this ok? Thanks, Bryan!
Typeoneerror
Well the reason I used self. was that the @synthesized setters can handle the memory management for you assuming you set them up as retain. Also using them makes it KVO compliant, although that may not be a big issue on the iphone as it is in some desktop development.
Bryan McLemore
In addition to the above reasonings I've read some talk about potential future optimizations for the @synthesized getters and setters, and that it was recommend to get in the habit of using them. Of course that's subject to a what-if, so take it with a grain of salt.
Bryan McLemore
I am synthsizing those view controllers FWIW. Was it wrong to not use "self" when referring to them?
Typeoneerror
I'm not sure I'd say 'wrong', there's seldom a 100% right or wrong answer. I'd recommend using it though.
Bryan McLemore
The exception to that would be if you've got a critical path and the usage of the setter created a unacceptable performance penalty.
Bryan McLemore