IOS Segue - When control instances of viewControllers are displayed

I have a simple storyboard with a Main View controller and two part view controllers, as shown in the diagram.

Question # 1 - where is the code that SequeA will represent DetailA located

Question # 2 . In my code for AppDelegate, I need to create an array of all ViewControllers that are in the application. How can I get this array? β€œIt's in the bulletin 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 and b)

Question number 3 . Is DetailA instantiated when the MainViewController is instantiated, or is it created when Seque fires (what is the correct word here - Called?)

StoryBoard with MainView Controller and Two Detail Views

+4
source share
3 answers

You asked:

Question number 1 . Where is the code that SegueA will introduce DetailA?

If you call segue A programmatically (for example, call performSegueWithIdentifier ), this code is in the main view controller. Often, however, you do not need to reference it programmatically at all, because when you create a segue in Interface Builder, you often associate it with some control, like a button on the main view, and thus you should not do anything either programmatically to initiate a session. However, when calling segue A, the optional associated shouldPerformSegueWithIdentifier (for iOS 6 and above) and prepareForSegue called in the main view controller.

Question # 2 . In my code for AppDelegate, I need to create an array of all ViewControllers that are in the application. How can I get this array? β€œIt's in the bulletin 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 and b)

As a rule, you do not need to support arrays of view controllers (with the possible exception of custom container view controllers, and even then, sometimes you do not need to do this yourself). But, if you need to access some property of your application delegate, you can do something like:

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

Having said that, it’s hard for me to think about situations where it is recommended for the part A controller or part B controller to get a list of view controllers from the host. You really need to explain what kind of business problem you are trying to solve. Typically, you are running some delegate protocol or using some kind of notification process. It depends on the problem you are solving. But you should carefully study your design if A or B needs to get a list of view controllers from the wizard.

Question number 3 . Is DetailA instantiated when the MainViewController is instantiated, or is it created when Seque fires (what is the correct word here - Called?)

With the exception of custom containers and / or embedded segments, the main process:

  • segue is triggered;
  • shouldPerformSegueWithIdentifier optionally called in iOS 6, if NO , then we stop here;
  • an instance of the destination controller is created; Called
  • prepareForSegue , which allows you to transfer information from the source controller to the destination controller;
  • then a view is associated with the destination view controller;
  • viewDidLoad in the receiver is called (take home message, do not try to manipulate the views / controls in this target view until this point, for example, in the prepareForSegue source);
  • only then will the representation of the addressee be displayed, its appearance, appearance, etc.

References:

0
source

1) If you connected it to IB, there is no code (except for the XML file that describes the storyboard).

2) You cannot access other controllers until you create them.

3) It is created when segue is executed.

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

+1
source

If you read the link, you will understand that there is no code to execute segue at your request., Xcode / Interface Builder) is not a code generator, as you can understand, if you have interface builders in other languages, it does not write code for you , so you can’t find him. In doing so, you can programmatically call a session.

As for listing ViewControllers, I don't believe there is a way to do this the way you hope. ViewControllers are only classes of a certain type. The only thing you can do is get a list of all the classes loaded and iterative, checking to see if they are view controllers, but that will be slow and I'm not sure why you want to do this, after all this the code will be executed to be included in your project, and, of course, do you know what code you have in your project?

View controllers are created during a session.

+1
source

All Articles