self.navigationController pushViewController不将控制器添加到堆栈 [英] self.navigationController pushViewController not adding controller to stack

查看:455
本文介绍了self.navigationController pushViewController不将控制器添加到堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个名为vcA的viewController上执行此操作:

I am on a viewController called vcA and I do this:

[self.navigationController pushViewController:vcB animated:YES];

并且有效。 vcB被推了。在vcB的viewDidAppear里面我这样做:

and it works. vcB is pushed. Inside vcB's viewDidAppear I do this:

NSArray *controllers = self.navigationController.viewControllers;

和控制器只包含一个对象,vcA !!!! (什么?)

and controllers contains just one object, vcA !!!! (what?)

为什么vcB被添加到控制器阵列?有没有什么可以防止这种情况发生?

Why is vcB being added to the controllers array? Is there anything that can prevent that from happening?

谢谢。

推荐答案

<为了解决这个问题,我把头撞在墙上一周,包括推动导航控制器堆栈上的视图控制器。

I banged my head on the wall for a week to solve this problem involving pushing a view controller on the navigation controller stack.

问题在于:我使用的是基于导航控制的应用程序,我有3个视图控制器,vA,vB和vC。

The problem was this: I was on a navigation controlled based app and I had 3 view controllers, vA, vB and vC.

我用vA从vA推vB:

I was pushing vB from vA using this:

[self.navigationController pushViewController:vB animated:YES];

它有效,但是,当我试图从vB推送vC时,vB的self.navigationController属性是零并且vB不在导航控制器堆栈上。什么?是的,我在导航堆栈上推了vB并且没有添加vB,但即便如此,vB也正确显示。

it worked, but, when I tried to push vC from vB, the self.navigationController property of vB was nil and vB was not on the navigation controller stack. What? Yes, I pushed vB on the navigation stack and vB was not being added to it, but even so, vB was showing correctly.

我发现在vB的viewDidLoad中,自我.navigationController不是nil并且vB在导航控制器堆栈上,但是一旦vB的viewDidLoad结束,vB就从导航控制器堆栈中删除,其navigationController属性被设置为nil。那时,vB是导航控制器堆栈外的一种幽灵控件。

I discovered that inside vB's viewDidLoad, self.navigationController was not nil and that vB was on the navigation controller stack, but as soon as vB's viewDidLoad ended, vB was removed from the navigation controller stack and its navigationController property was set to nil. At that time, vB was a kind of ghost control, outside the navigation controller stack.

我不需要提及vB我无法回到vA或推送vC。

I don't need to mention that from vB I was unable to get back to vA or to push vC.

怎么会这样?

简单,我在一个区块内推动vB ......类似于:

simple, I was pushing vB from inside a block... something like:

void (^ block1)() = ^(){

   [self.navigationController pushViewController:vB animated:YES];

};

发生了什么事,因为UIKit函数(推送事物)正在一个块中执行在不是主线程的线程上运行。

What was happening because a UIKit function (the push thing) was being executed in a block that was probably running on a thread that was not the main thread.

解决方法是将推送包装到主线程中,使用:

The resolution to that was to wrap the push in a dispatch to the main thread, using this:

void (^ block1)() = ^(){
dispatch_async(dispatch_get_main_queue(),
               ^{
                   [self.navigationController pushViewController:vB animated:YES];
               });

};  

该应用程序还有另一个小问题,连接到viewController的插座错误,但这是主要问题。这是一个难以发现的问题,因为它是微妙的。当我向导航栏添加另一个按钮并且没有出现该按钮时,我发现了这个问题。所以,我怀疑涉及UIKit的事情正在发生,或者在这种情况下没有发生。这里的一个大问题是这段代码没有崩溃,没有错误信息,没有,只是工作不好,但工作。

The app had another minor problem with a wrong outlet connected to a viewController, but this was the main problem. This is the kind of problem hard to spot, for being subtle. I discovered the problem when I added another button to the navigation bar and that button did not appear. So, I suspect that something involving UIKit was happening, or not happening in that case. The big problem here is that this code was not crashing, no error message, nothing, just working bad, but working.

这篇关于self.navigationController pushViewController不将控制器添加到堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆