解除控制器时,在presentModalViewController中调用dealloc方法

时间:2010-03-25 05:53:56

标签: iphone objective-c

此处以下代码用于查看当前的模态视图控制器。

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

1 个答案:

答案 0 :(得分:2)

当对象被释放时,调用Dealloc方法,因为它被保留了相同的次数。 当您将doctorListView ...(让我们称之为视图)添加到navigationController(让我们称之为控制器)时,控制器会保留视图。它在创作过程中也得到了保留。这就是为什么你应该发布这个视图两次:一次使用dismissModalView ......一次是直接释放。

我的意思是这样的:

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

...

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