1

目前,我按以下方式设置了导航堆栈:

ViewController A-> ViewController B-> ViewController C->ViewController D

ViewControllerC 和的实现代码中ViewController D,我有一个选择器,只有在满足以下测试时才会运行代码块

if (self.navigationController.visibleViewController)

我面临的问题是上述测试总是返回 true for ViewController C。首先,我在 IOS 模拟器中启动我的应用程序,然后向上导航到 View Controller D。ViewController D加载后,我按下模拟器上的“主页”按钮。按下主页按钮后,我再次单击我的应用程序并重新启动我的应用程序(进入前台)。现在发生的事情很奇怪,我可见的视图控制器是ViewController D,因为那是我上次使用的视图控制器。虽然ViewController D现在是目前唯一对我可见的视图控制器,但上面的 if 语句也返回 true ViewController C!(我只想ViewController D's执行选择器代码,这是一个问题)。

所以这引出了一个问题,成为 a 到底意味着什么,如果是出现在我面前的视图控制器,visibleViewController我怎样才能使ViewController C's选择器不执行.. 谢谢!ViewController D

4

2 回答 2

6

The visibleViewController is a property that returns the controller that is currently visible, not a boolean property returning YES or NO depending on whether or not the current controller is visible. As long as there is a visible view controller on the screen - any controller at all - the check of self.navigationController.visibleViewController will return YES, because any non-nil value passed to an if is considered a YES.

The check should be as follows:

if (self == self.navigationController.visibleViewController)

The comparison will return YES if the current view controller is the visible view controller of the navigation controller, and NO otherwise.

于 2013-08-02T01:00:31.667 回答
1

visibleViewController will always return an object (at least with your setup) which makes your if statement always true -- it doesn't return true only if the controller you have that code in is on screen. Instead you should use self.view.window as the test. It will only return true if self's view is on screen.

You could also still use self.navigationController.visibleViewController, but compare it to self to see if they're the same.

于 2013-08-02T01:00:03.500 回答