iOS Segue - viewControllers 何时实例化 [英] iOS Segue - When are the viewControllers Instantiated

查看:23
本文介绍了iOS Segue - viewControllers 何时实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的故事板,其中有一个主视图控制器和两个细节视图控制器,如图所示.

I have a simple storyboard with a Main View Controller and two Detail view Controllers as shown in the diagram.

问题 #1 - SequeA 将呈现 DetailA 的代码位于何处

Question #1 - Where is the code that SequeA will present DetailA located

问题 #2 - 在我的 AppDelegate 代码中,我需要创建一个包含应用程序中所有 ViewController 的数组 - 我如何获得该数组?- 它在故事板中,但我如何以编程方式访问它.

Question #2 - In my code for the AppDelegate I need to create an array of all the ViewControllers that are in the App - How can I get that array? - It is in the story board but how do I programmatically access it.

我可以通过执行以下操作来获取 MainViewController -myViewControllerMain = (ViewControllerMain*) self.window.rootViewController;

I can get the MainViewController by doing the following - myViewControllerMain = (ViewControllerMain*) self.window.rootViewController;

但我不知道如何访问详细视图控制器 (a & b)

but I don't know how to access the detail view controllers (a & b)

问题 #3 - DetailA 是在 MainViewController 实例化时实例化还是在 Seque 被触发"时实例化(这里正确的词是什么 - 调用?)

Question #3 - Is DetailA instantiated when the the MainViewController is instantiated or is it instantiated when the Seque is "triggered" (what is the right word here - Called?)

推荐答案

你问:

问题 #1 - SegueA 将显示 DetailA 的代码在哪里?

Question #1 - Where is the code that SegueA will present DetailA located?

如果您以编程方式调用 segue A(例如调用 performSegueWithIdentifier),则该代码位于主视图控制器中.但是,通常您根本不需要以编程方式调用它,因为当您在 Interface Builder 中创建 segue 时,您通常是从某个控件(例如主视图中的按钮)链接它,因此您不需要必须以编程方式做任何事情来启动 segue.但是,当您调用 segue A 时,在主视图控制器中调用可选的关联 shouldPerformSegueWithIdentifier(对于 iOS 6 及更高版本)和 prepareForSegue.

If you're invoking segue A programmatically (e.g. calling performSegueWithIdentifier), that code is in the master view controller. Often, though, you don't need to invoke it programmatically at all, because when you create the segue in Interface Builder, you're often linking it from some control, like a button in the master view, and thus you don't have to do anything programmatically to initiate the segue. When you invoke segue A, though, the optional associated shouldPerformSegueWithIdentifier (for iOS 6 and higher) and prepareForSegue are called in master view controller.

问题 #2 - 在我的 AppDelegate 代码中,我需要创建一个包含应用程序中所有 ViewController 的数组 - 我如何获得该数组?- 它在故事板中,但我如何以编程方式访问它.

Question #2 - In my code for the AppDelegate I need to create an array of all the ViewControllers that are in the App - How can I get that array? - It is in the story board but how do I programmatically access it.

我可以通过执行以下操作来获取 MainViewController -

I can get the MainViewController by doing the following -

myViewControllerMain = (ViewControllerMain*) self.window.rootViewController;

但我不知道如何访问详细视图控制器 (a & b)

but I don't know how to access the detail view controllers (a & b)

通常您不需要维护视图控制器数组(自定义容器视图控制器可能例外,即使如此,有时您也不必自己做).但是,如果您需要访问应用委托的某些属性,您可以执行以下操作:

Generally you don't need to maintain arrays of view controllers (with the possible exception of custom container view controllers, and even then, sometimes you don't have to do it yourself). But, if you need to access some property of your app delegate, you can do something like:

YourAppDelegate *appDelegate = (YourAppDelegate *)[UIApplication sharedApplication].delegate;
// you can now access properties of the `appDelegate`

话虽如此,我很难想到在哪些情况下,细节控制器 A 或细节控制器 B 从主控器检索视图控制器列表是可取的.您确实应该解释您要解决的业务问题.通常你会做一些委托协议或使用一些通知过程.它因您要解决的问题而异.但是,如果 A 或 B 需要从主控器获取视图控制器列表,您应该仔细查看您的设计.

Having said that, I'm hard-pressed to think of situations where its advisable for Detail Controller A or Detail Controller B to be retrieving a list of view controllers from the master. You really should explain what business problem you're trying to solve. Generally you'd do some delegate protocol or use some notification process. It varies based upon the problem you're solving. But you should take a hard look at your design if A or B needs to get a list of view controllers from the master.

问题 #3 - 是在 MainViewController 实例化时实例化 DetailA 还是在触发"Seque 时实例化它?(这里的正确词是什么 - 叫?)

Question #3 - Is DetailA instantiated when the the MainViewController is instantiated or is it instantiated when the Seque is "triggered" (what is the right word here - Called?)

除了自定义容器和/或嵌入转场,基本流程是:

With the exception of custom containers and/or embed segues, the basic process is:

  • segue 被触发;
  • shouldPerformSegueWithIdentifier 在 iOS 6 中是可选调用的,如果 NO 那么我们就到此为止;
  • 目标控制器已实例化;
  • prepareForSegue 被调用,允许你将信息从源控制器传递到目标控制器;
  • 然后创建与目标视图控制器关联的视图;
  • 目标中的
  • viewDidLoad 被调用(带回家的消息是,在此之前不要尝试操作此目标视图中的视图/控件,例如在源的 prepareForSegue);
  • 只有这样,目标视图才会完成其布局、外观调用等.
  • the segue is triggered;
  • shouldPerformSegueWithIdentifier is optionally called in iOS 6, if NO then we stop here;
  • the destination controller is instantiated;
  • prepareForSegue is called, allowing you to pass information from the source controller to the destination controller;
  • the view associated with the destination view controller is then created;
  • viewDidLoad in the destination is called (the take home message being, don't try to manipulate the views/controls in this destination view prior to this point, such as in the source's prepareForSegue);
  • only then will the destination view complete its layout, appearance calls, etc.
  • View Controller Programming Guide is a good overview of storyboards, view controllers, etc.

这篇关于iOS Segue - viewControllers 何时实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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