iOS Segue - When are the viewControllers Instantiated

青春壹個敷衍的年華 提交于 2019-12-23 13:08:59

问题


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

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

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.

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

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

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:


You asked:

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

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.

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.

I can get the MainViewController by doing the following -

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

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`

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.

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:

  • 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.

References:

  • View Controller Programming Guide is a good overview of storyboards, view controllers, etc.



回答2:


If you read the link, you will understand that there is no code to perform the segue as you asked . . . the XCode / Interface Builder) is not a code generator as you may understand it if you have use interface builders in other languages, it is not writing code for you, so there is none to be found. That being said, you can programmatically call a segue.

As for listing out the ViewControllers, I don't believe there is a way to do this in the manner you're hoping. ViewControllers are just classes of a particular type. The only thing you could do is to get a list of all classes loaded and iterate though checking if they are view controllers, but this would be slow and I'm not sure why you would want to do it, after all the only way this code would be executed would be to be included in your project and surely you know what code you have in your project?

View controllers are instantiated when the segue is performed.




回答3:


1) If you hooked it up in IB, there is no code (other than the XML file that describes the storyboard).

2) You can't get access to the other controllers until you instantiate them.

3) It's instantiated when the segue is performed.

Why do think you need to create an array of all your controllers in the app delegate?



来源:https://stackoverflow.com/questions/14607581/ios-segue-when-are-the-viewcontrollers-instantiated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!