【问题标题】:Is dealloc method called in presentModalViewController when dismiss the controller关闭控制器时是否在 presentModalViewController 中调用了 dealloc 方法
【发布时间】:2010-03-25 05:53:55
【问题描述】:

这里下面的代码用于查看当前的模态视图控制器。

[[self navigationController] presentModalViewController:doctorListViewNavigationController animated:YES];

关闭动作在下一个视图控制器(DoctorListViewController)中。看我现在加的下面代码就明白了。

-(void)doctorsListAction
{
    if(isFirst == YES)
    {
      [self getDoctorsListController];
      [[self navigationController] presentModalViewController:doctorListViewNavigationController animated:YES];

    }
}

-(void)getDoctorsListController
{
    DoctorListViewController *doctorListViewController=[[DoctorListViewController alloc]init];
    doctorListViewController.doctorList=doctorList;
    doctorListViewNavigationController=[[UINavigationController alloc]initWithRootViewController:doctorListViewController];
    doctorListViewNavigationController.navigationBar.barStyle=  UIBarStyleBlackOpaque;
    [doctorListViewController release];

//code in next DoctorListViewContrller to dismiss the view.
//code for dismiss the ModalViewController.
-(void)closeAction
{
    [[self navigationController] dismissModalViewControllerAnimated:YES];
}

我的问题是没有调用 dealloc 方法,然后我遇到了内存问题,例如对象分配、泄漏……

- (void)dealloc 
{

    [doctorList release];
    [myTableView release];
    [super dealloc];
}

【问题讨论】:

  • 你是如何创建医生列表视图导航控制器的?
  • 请修正您的帖子。此代码目前无法读取。

标签: iphone objective-c


【解决方案1】:

当对象被释放的次数与被保留的次数相同时,会调用 Dealloc 方法。 当您将医生列表视图...(我们称其为视图)添加到导航控制器(我们称其为控制器)时,控制器会保留该视图。并且在创作过程中也被保留了下来。这就是为什么你应该释放这个视图两次:一次是用dismissModalView...,一次是直接释放。

我的意思是这样的:

  [[self navigationController] presentModalViewController:doctorListViewNavigationController animated:YES];
  [doctorListViewNavigationController release];  // first time

...

- (void)closeAction {
  [[self navigationController] dismissModalViewControllerAnimated:YES];
               // second time
}

【讨论】:

  • 这只有在你已经拥有doctorListViewNavigationController 时才是正确的,例如,如果你像这样创建它:MYDoctorListViewNavigationController * doctorListViewNavigationController = [[doctorListViewNavigationController alloc] initWithNibname:@"something" bunlde:nil];(诚然,这很可能是这种情况,但很好指出,因为它似乎是提问者出错的地方)。
  • 感谢您的回复,我更清楚地编辑以了解我的问题
  • 关闭操作在其他视图中,即 DoctorListView 那么我该如何再次释放它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多