-1

假设我有两个视图控制器,分别是 vc1 和 vc2。每个vc都有一个按钮,当按下时,背景将从默认的白色变为黑色,以及一个下一步按钮。

所以vc1的背景变黑了,点击next按钮,vc2就没有问题了。

- (IBAction)nextVC:(id)sender {

    UIViewController *otherView = [self.storyboard instantiateViewControllerWithIdentifier:@"vc2"] ;
    [self presentViewController:otherView animated:YES completion:NULL] ;
}

我在vc2上做同样的事情,因此vc2的背景变黑了。然后我关闭 vc2,回到 vc1,vc1 的背景保持黑色。

[self dismissViewControllerAnimated:YES completion:nil] ;

问题是,当我按下vc1上的next按钮时,vc2的背景颜色不是保持黑色,而是默认为白色。有没有办法可以恢复我已经解雇的 vc2 的状态?无需保存数据或在 viewWillApper 等上设置任何内容。

简单地说,我要求一种方法来恢复被关闭的视图,但不推送新视图(vc2)。

谢谢您的帮助。

4

2 回答 2

2

如果您检查您的代码nextVC,您正在创建第二个视图控制器的新实例并推送它。这就是为什么你总是得到刚刚初始化的白色背景 VC。

所以,如果你想保持你的第二个 VC 的状态,你需要继续引用它。

添加UIViewController *otherView到您的视图控制器 .h 文件或 .m 作为本地属性。然后更改您的推送功能。

//Only initialise first time. Later don't need
if (otherView == nil){
    otherView = [self.storyboard instantiateViewControllerWithIdentifier:@"vc2"] ;
}
[self presentViewController:otherView animated:YES completion:NULL] ;

它应该工作。

于 2013-09-12T10:20:01.203 回答
-1

您不能只恢复“Dismissed”视图控制器,因为当 viewController 被解除时,它会从内存中删除并且其保留计数变为 0。您的 vc1 保持其状态,因为它永远不会被解除,您只是在呈现另一个控制器在上面。如果要保存 vc2 的状态,可以在 SharedInstance 或 AppDelegate 中设置一个标志,然后在出现 vc2 时检查它。

于 2013-09-12T10:19:55.030 回答