1
在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」之一。我想知道的是,如果我需要釋放這個「本地」變量,或者它可以獨自離開,因爲它只是指向我的一個視圖控制器(我在dealloc中發佈)。我不認爲「標籤」需要被釋放,因爲它是「原始的」,是正確的?按鈕怎麼樣?我不太明白什麼應該和不應該明確發佈,所以也許我會過於謹慎。提前致謝。
是的,它看起來像這樣:social = [[SocialViewController alloc] initWithNibName:@「SocialViewController」bundle:nil]; – typeoneerror 2010-02-03 15:17:16
我不認爲我需要「自我」。我呢?完全假定範圍默認爲「自我」。這裏。此外,我沒有使用autorelease,因爲我保留和釋放,然後明確地在這個方法和dealloc。這個可以嗎?謝謝,布萊恩! – typeoneerror 2010-02-03 15:18:25
那麼我使用自我的原因。假設你將它們設置爲保留,那麼@synthesized setter可以爲你處理內存管理。同樣使用它們使其符合KVO標準,儘管這在iphone中可能不是什麼大問題,因爲它在某些桌面開發中。 – 2010-02-04 01:49:13