从父视图控制器中移除子视图控制器。

3

我有一个父视图,像这样打开一个子视图:

ChildViewController *child = [[ChildViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:child animated:YES];

这个很好运作。我需要从父视图中关闭子视图,但是当我这样做时,什么也没有发生。这是因为当我打开子视图时,父视图停止了所有的进程吗?还是我的代码有问题:[child dismissModalViewControllerAnimated:YES];?谢谢。

2个回答

3

dismissModalViewControllerAnimated: 必须在调用 presentModalViewController:animated: 的同一对象上调用。

在你的示例中,需要使用 [self dismissModalViewControllerAnimated:YES];

如果你是从被模态显示的控制器内部进行解除,则应如@James Bedford所述使用 [[self parentViewController] dismissModalViewControllerAnimated:YES];


谢谢你为我澄清这个问题。我以为应该从子视图中调用self dismissModalViewControllerAnimated,但看来不是这样。 - Preston

1
你在哪里调用 [child dismissModalViewControllerAnimated:YES];?这行代码是否被执行过?
你可以在 ChildViewController 类中的一个 UIControl 上添加 target/action,使用继承的 parentViewController 属性来关闭自身,如下所示: [[self parentViewController] dismissModalViewControllerAnimated:YES];

代码行从父级被调用,但子级没有关闭。我在那里放了一个NSLog并且它出现在控制台中,所以我知道它是从父级被调用的。 - Preston

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接