[英]Get ViewController from Appdelegate
I have four views in my storyboard 我的故事板中有四个视图
In my ViewTwo
, I make a call and open an new App and when the URL opening is done I call the following function in my Appdelegate
在我的
ViewTwo
,我打电话并打开一个新的应用程序,当URL打开完成后,我在Appdelegate
调用以下函数
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool
What I do here is that I iterate through all my viewControllers because I want to find my ViewTwo
to be able to call a segue that I have created. 我在这里所做的是,我要遍历所有viewControllers,因为我想找到
ViewTwo
以便能够调用我创建的segue。 I want to do it this way because I don´t want to create a new instance of the viewController. 我想这样做,因为我不想创建viewController的新实例。
It works great with this function below to find ViewTwo
: 通过下面的此功能可以很好地找到
ViewTwo
:
if let viewControllers = window?.rootViewController?.childViewControllers {
for viewController in viewControllers {
if viewController.isKindOfClass(ViewTwo) {
viewController.performSegueWithIdentifier("sw", sender: self)
}
}
}
But now I have added a NavigationController
to ViewTwo
, ViewThree
and ViewFour
so when I run the snippet above I only get the following result for viewController (I made a simple print(viewController)
) 但是现在我已经向
ViewTwo
, ViewThree
和ViewFour
添加了NavigationController
,因此当我运行上面的代码片段时,我只会得到viewController的以下结果(我做了一个简单的print(viewController)
)
<ViewOne: 0x...>
<UINavigationController: 0x...>
<UINavigationController: 0x...>
<UINavigationController: 0x...>
So my question is, how do I check for ViewTwo now that I also have a NavigationController? 所以我的问题是,既然我也有NavigationController,如何检查ViewTwo?
I solved this by 我解决了
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("ViewThree") as! WebViewController
self.window?.rootViewController!.presentViewController(controller, animated: true, completion: { () -> Void in
})
You can get the view controller from storyboard without creating a new instance. 您可以从情节提要中获取视图控制器,而无需创建新实例。 You don't need to iterate through all the view controllers.
您不需要遍历所有视图控制器。 Also why do you not set the view controller as the root of your nav controller instead of performing a segue?
另外,为什么不将视图控制器设置为导航控制器的根目录而不是执行segue?
You can get the view controller from storyboard like this: 您可以从情节提要中获取视图控制器,如下所示:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("ViewTwo") as! ViewTwoViewController
Because now you are all getting UINavigationViewController
. 因为现在您都可以使用
UINavigationViewController
。 And it's just simply a controller of controller. 它仅仅是控制器的控制器。 You can access the root view controller from
UINavigationViewController
by accessing the first element of viewControllers
property. 您可以通过访问
viewControllers
属性的第一个元素,从UINavigationViewController
访问根视图控制器。 Then, you are able to make the interpolation. 然后,您可以进行插值。
The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.
根视图控制器位于数组的索引0中,后视图控制器位于索引n-2处,而顶部控制器位于索引n-1中,其中n是数组中的项数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.