开发者

Releasing "referential" variables in method scope

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 = [[SocialViewContr开发者_StackOverflow社区oller 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.


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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜