iphone编程+从一个视图转移到另一个泄漏的内存

时间:2011-06-29 21:27:35

标签: iphone objective-c memory memory-leaks

我有一个类MarshViewController.h,我使用以下代码从一个viewcontroller移动到另一个。

#import "ExpertGameController.h"
@interface MarshViewViewController : UIViewController<UIAlertViewDelegate> {
ExpertGameController *nextExpertGame;
}
@property(nonatomic,retain)ExpertGameController *nextExpertGame;

在.m我合成了它,我正在使用以下方法。我已经发布了nextExpertGame但仍然泄漏了内存

-(IBAction)expertGame
{

    nextExpertGame=[[ExpertGameController alloc]initWithNibName:@"ExpertGameController" bundle:nil];
     [self.navigationController pushViewController:nextExpertGame animated:YES];
    [nextExpertGame release];

}

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

当你分配控制器时你给它一个保留计数为1,当你把它推到导航控制器时你给它一个保留计数为2然后当你释放它时你把保留计数降回到1。从导航控制器弹出该视图控制器后,保留计数将降为0,然后将从内存中释放。

答案 1 :(得分:0)

从一个视图移动到另一个视图的简单方法如下所示:

#import "ExpertGameController.h"
@interface MarshViewViewController : UIViewController<UIAlertViewDelegate> {

      //Your declared variables
}


-(IBAction) expertGame {

ExpertGameController *objExpertGameController = [[ExpertGameController alloc]  initWithNibName:@"ExpertGameController" bundle:nil]; 

[self.navigationController pushViewController: objExpertGameController animated:YES];
[objExpertGameController release];

}

希望这有帮助!