代码之家  ›  专栏  ›  技术社区  ›  typeoneerror

在方法作用域中释放“引用”变量

  •  1
  • typeoneerror  · 技术社区  · 15 年前

    在objective-c(cocoa touch)中,我有一系列的uiviewcontrollers可以在它们之间切换。

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

    如您所见,我正在将一个视图控制器分配给“nextviewcontroller”。我想知道的是,我是否需要释放这个“local”变量,或者是否可以不使用它,因为它只指向我的一个视图控制器(我在dealloc中释放)。我不认为“标签”需要释放,因为它是一个“原始的”,对吧?巴顿怎么样?我不太明白应该和不应该明确发布什么,所以我可能是过于谨慎了。提前谢谢。

    1 回复  |  直到 15 年前
        1
  •  1
  •   Bryan McLemore    15 年前

    一般来说你只需要 release 一个变量 retain D init D或 copy D

    编辑:

    在多读了一点代码之后,您可能会遇到其他错误值的问题。下面的代码对我来说更有意义。这假设财务、社会和票务 @synthesized 伊瓦尔斯

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