0

我在 xcode 中有一个使用情节提要的项目。加载的第一个视图是“接受条款和条件”视图,其中用户必须单击接受按钮才能继续。单击它后,它会转到下一个视图。在程序第一次启动时用户单击接受后,我不再希望他们再次看到该视图 - 我希望它直接进入下一个视图。我有一些代码,但它不工作。这就是我所拥有的:

在应用程序委托中:(在 applicationDidFinishLaunchingWithOptions 内)

if([[NSUserDefaults standardUserDefaults] boolForKey:@"TermsAccepted"]!=YES)
{
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"TermsAccepted"];
}

内部接受条款和条件视图实现:(viewDidLoad)

 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"TermsAccepted"]){
    [self.navigationController pushViewController: self animated:YES];
    //I want it to go to the next screen
}
    else {
        //I want to show this screen, but I don't know what goes here
}

同样在接受条款和条件视图实现中(在接受按钮中)

 - (IBAction)acceptButton:(id)sender {
 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"TermsAccepted"];
}

我运行它并得到错误:'不支持多次推送同一个视图控制器实例'。我该如何解决?

4

2 回答 2

1
  1. 在您的第一个代码片段中,您基本上说“如果 TermsAccepted 不是 YES(所以它是 NO),则将其设置为 NO。这没有意义
  2. 在您的第二个代码片段中,您编写了[self.navigationController pushViewController:self animated:YES];. 所以基本上你要求 current UIViewController( self) 把自己推到它自己的 navigationController 上……这也没有意义。

这就是为什么你有这个错误。您尝试推送当前的 viewController self,而它已经在您的 navigationController 的屏幕上。所以你尝试在self同一个 navigationController 上推送同一个实例 ( ) 两次。

You obviously meant to push another viewController (probably an instance of a TermsAndConditionViewController or something that shows the terms and conditions of your app) on the navigation controller, and not the current viewController itself, which doesn't make sense.

于 2012-10-14T16:24:21.803 回答
0

First, you want to have the next view controller, the one you always want to show, be the root view controller of your window. In that controller's viewDidLoad method, put your if clause to show the accept terms and conditions controller -- you can show that one using presentModalViewController. The if clase can be like this:

If([[NSUserDefaults standardUserDefaults] BoolForKey:@"TermsAccepted"] !=YES) {
    // instantiate your terms and conditions controller here
    // present the controller
}

Then, in the method where you dismiss the terms and conditions controller, set the value of that key to YES.

于 2012-10-14T20:46:14.160 回答