简体   繁体   English

iOS如何知道哪个视图控制器应该处于活动状态?

[英]How does iOS know which view controller should be active?

If I want to replace one screen of an app with another, but I don't use a navbar/tabbar controller, then I could just remove oldViewController.view from window and add newViewController.view to it. 如果我想用另一个屏幕替换一个应用程序的屏幕,但是我不使用导航栏/标签栏控制器,那么我可以从窗口中删除oldViewController.view并向其中添加newViewController.view。 That's all, now newViewController will get rotation events, etc. 就是这样,现在newViewController将获得旋转事件,等等。

But UIView does not reference "its" controller, so how is this possible, how iOS know it should make newViewController an active one? 但是UIView没有引用“它的”控制器,那么怎么可能,iOS如何知道它应该使newViewController成为活动的控制器呢? Does iOS do some magic, it internally references controller from view or what? iOS会做些魔术吗,它从视图内部引用控制器还是什么?

UPDATE: 更新:

I think I was misunderstood: I don't ask how to make some view controller an active one - I know that. 我想我被误解了:我不问如何使某个视图控制器成为活跃的视图控制器-我知道。 I'm just curious, how is it possible that I pass some view to UIWindow object ([window addSubview:view]) and it somehow finds view controller although view doesn't know its controller. 我只是很好奇,我怎么可能将一些视图传递给UIWindow对象([window addSubview:view]),尽管视图不知道其控制器,但它还是以某种方式找到了视图控制器。

yeh I had the same question like you. 是的,我也有同样的问题。 and I figured it out. 我想通了

UIView is derived from UIResponder. UIView派生自UIResponder。 and UIView must subclass UIResponder::nextResponder. 并且UIView必须子类化UIResponder :: nextResponder。

Its default implementation is returning a view controller of the view (if it hadn't, it would be super view) 它的默认实现是返回视图的视图控制器(如果没有,则为超级视图)。

So, consequently view can see its controller. 因此,因此视图可以看到其控制器。 that means window know the topmost view and also 这意味着窗口知道最顶层的视图

its controller. 它的控制器。

good luck. 祝好运。

Unfortunately, iOS only send events to the first ViewController of the stack. 不幸的是,iOS仅将事件发送到堆栈的第一个ViewController You can try and present a new one on the top of others with video for example, it will never rotate. 例如,您可以尝试在其他视频的顶部展示一个新视频,它永远不会旋转。

If you don't use navbar/tabbar controller you will have to add and remove everytime from the Window to keep only one at the time if you wand to have events. 如果您不使用导航栏/标签栏控制器,则每次必须从Window添加和删​​除它,以便在每次发生事件时仅保留一次。

The main UIWindow class for your application will have a view controller set in its rootViewController property. 应用程序的主UIWindow类将在其rootViewController属性中设置一个视图控制器。 That controller's view is the "main" view for the app. 该控制器的视图是该应用程序的“主”视图。 This is usually setup in the main .xib for the project. 这通常是在项目的主.xib中设置的。 That view controller will receive the usual events like "viewDidAppear" or "willRotateToInterfaceOrientation". 该视图控制器将接收到诸如“ viewDidAppear”或“ willRotateToInterfaceOrientation”之类的常见事件。 You can put up your own view over top of it if you want to, but you will need to manage those events yourself. 您可以根据需要在视图之上放置自己的视图,但是您需要自己管理这些事件。 Usually you don't do that though. 通常,您通常不会这样做。 You just use a UINavigationController or UITabBarController as your rootViewController and allow them to manage getting the events to new "pushed" view controllers, or you popup view controllers with "presentModalViewController". 您只需将UINavigationController或UITabBarController用作rootViewController,并允许它们管理将事件发送到新的“推送”视图控制器,或者使用“ presentModalViewController”弹出视图控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道iPhone中哪个按钮称为视图控制器? - how to know which button called view controller in iPhone? 如何知道视图控制器是否在堆栈中。 如果它在堆栈中,则应将其弹出,否则应将其压入 - How to know whether a view controller is in stack. If it in stack it should be popped or else it should be pushed 如何检测iOS中弹出了哪个子视图控制器? - How to detect which child view controller was popped up in iOS? 我应该使用哪个视图控制器? - Which view controller should I be using? 如何知道视图堆栈中的最新视图控制器 - How to know the latest view controller in the view stack 如何在使用导航控制器时以编程方式知道当前在屏幕上出现的视图 - how to know programmatically which view is presently appears on screen while using navigation controller 如何知道视图控制器是作为弹出窗口还是模态显示? - How to know if a view controller will be presented as a popover or modal? 页面视图控制器是否支持iOS 4.3 - Does page view controller supports iOS 4.3 如何在iOS中查看表视图中的选定开关 - how to know selected switch in table view in iOS 在iPhone上使用模态视图控制器编辑属性时应使用哪种模式? - Which pattern should be used for editing properties with modal view controller on iPhone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM