2011-04-28 50 views
0

這是一個擴展我的問題here何時釋放選項卡欄應用程序中的對象?

下面是這種情況:

我有三個標籤,其中開關是存在的。當我觸摸開關時,標籤被更新(ON或OFF),並且燈泡的圖像從一個JPG變爲另一個。所以我正在使用UIImageView,其中我正在更改UIImage。

#import <UIKit/UIKit.h> 
@interface FirstViewController : UIViewController { 
    UISwitch *switch1; 
    UILabel *status1; 
} 

@property (nonatomic,retain) IBOutlet UISwitch *switch1; 
@property (nonatomic,retain) IBOutlet UILabel *status1; 
- (IBAction) switch1Change; 
@end 

和實施

#import "FirstViewController.h" 
@implementation FirstViewController 
@synthesize switch1; 
@synthesize status1; 
- (IBAction) switch1Change 
{ 
    if (switch1.on) { 
     status1.text = @"ON"; 
     ... 
    } 
    else { 
     status1.text = @"OFF"; 
     ... 
    } 
} 

我無法理解時釋放的對象。我試着在 - (void)dealloc {}方法中給出[switch1 release]。但是當我切換標籤時,應用程序崩潰。這個怎麼做?

+1

Crash log plz? – Jhaliya 2011-04-28 05:44:18

+0

我懷疑你的崩潰與放置在[class dealloc]中的'[switch1 release]無關,因爲它只在有內存警告時被調用。我建議仔細觀​​察運行日誌並在調試模式下工作,看看導致崩潰的實際錯誤是什麼。 – 2011-04-28 05:44:38

+0

在您的dealloc方法(或ViewDidUnload)中釋放保留/複製的對象(如您的IBOutlets中)是正確的。有關更多詳細信息,請參閱http://stackoverflow.com/questions/3965399/memory-management-in-objective-c和http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html。 。你是否刪除了發佈聲明並且沒有崩潰?我傾向於認爲別人會崩潰你的應用程序... – tsakoyan 2011-04-28 05:57:21

回答

0

[switch1 release] dealloc似乎是正確的,你得到了什麼崩潰?這可能與此無關。

如果您設置了一個斷點,您可能會發現這些標籤不會被取消分配;一旦由標籤欄加載,它會讓他們周圍。所有選項卡的視圖控制器都在開始時加載。

相關問題