2

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

在 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

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

4

3 回答 3

3

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

于 2012-08-16T11:42:58.143 回答
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];
}

希望这是你想要的。

祝你好运。

于 2012-08-16T08:56:12.133 回答
1

尝试这个,

- (IBAction)menuButtonPressed:(id)sender
{
   [self dismissModalViewControllerAnimated:YES];
}
于 2012-08-16T08:57:19.077 回答