解雇模态视图控制器

时间:2012-08-16 08:45:50

标签: iphone objective-c ios cocoa-touch modalviewcontroller

我有一个根视图控制器,用作菜单。选择项目时,它会以模态方式显示一些全屏数据。点击后退按钮时,将执行以下代码:

在BoardViewController.m中:

 - (IBAction)menuButtonPressed:(id)sender
    {
         [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    }

它很好地回归主菜单。但在此之后我想要被解雇的视图控制器被销毁(就像当你使用推/弹视图控制器时)。我没有存储它们的任何参考,但它们在解雇后仍然存在。我该如何解决? (使用ARC。)

修改

在AppDelegate.m中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...
    MenuViewController *menuVC = [[MenuViewController alloc] init];
    self.window.rootViewController = menuVC;
    ...
}

在MenuViewController.m中:

- (IBAction)newGame:(id)sender
    {
        BoardViewController *boardVC = [[BoardViewController alloc] init];
        boardVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:boardVC animated:YES completion:nil];
    }

编辑2

嗯,一个非弱的委托属性导致了这个问题。谢谢大家!

3 个答案:

答案 0 :(得分:3)

我不使用ARC,但是如果没有释放模态控制器,那么可能是因为其他东西仍然有引用它。模态控制器是否将自己添加为任何东西的委托?

答案 1 :(得分:2)

呈现ModalViewController应该在代码中看起来像这样:

- (void)showModal
{
    MyModalVC *mmvc = [[MyModalVC alloc] init];

    mmvc.dismissDelegate = self;

    UINavigationController *navController = [[UINavigationController alloc]
                                             initWithRootViewController:mmvc];

    navController.modalPresentationStyle = UIModalPresentationFormSheet; //or similar

    [self presentModalViewController:navController animated:YES];

    [cleaningTaskVC release]; //see that it is released after the presentation so that when you dismiss it you don't have to worry about the destruction of the object anymore
    [navController release];
}

最后的释放将确保销毁,以便您在解雇时不必担心。

这是我解雇它的方式(使用我在ModalVC类中使用的协议和委托),之后没有ModalVC的实例

- (void)didDismissModalView
{
    [self dismissModalViewControllerAnimated:YES];
}

希望这就是你想要的。

祝你好运。

答案 2 :(得分:1)

试试这个,

- (IBAction)menuButtonPressed:(id)sender
{
   [self dismissModalViewControllerAnimated:YES];
}
相关问题