0

I have an application which has a single window controller and 2 view controllers.I have created a segue from a button within the main View Controller to the second View Controller;to show a modal window.

Is it possible to access the controls located within the second view controller by creating an outlet within the .swift file of the first view controller. i.e.:access controls within the second view controller from the first view controller.

techno
  • 6,100
  • 16
  • 86
  • 192
  • The question is sort of unclear but from what I get out of it you essentially want to transfer data from the first view ccontroller to the second one. Is this correct? – TNguyen Sep 19 '17 at 19:29
  • @TPN1994 yes .. correct. – techno Sep 19 '17 at 19:30
  • what you can do is define variables inside of the second view controller and then on the segue from the first view controller to the second one you set the values of those variables and then the second view controller can just use it – TNguyen Sep 19 '17 at 19:31
  • @TPN1994 I have linked the segue by dragging and dropping.How can I pass values through it? Tried overriding in the view controller ... But the function does not exist. – techno Sep 19 '17 at 19:35
  • have a look at this SO thread here, It might not be exactly word for word what you want but I believe it's clear to see where to go after reading what's inside https://stackoverflow.com/questions/25215476/how-do-you-pass-data-between-view-controllers-in-swift – TNguyen Sep 19 '17 at 19:37
  • @TPN1994 Thanks but i have already tried overriding the segue function `override func prepareForSeguen` but it seems the there is no such function to override and an error appears. – techno Sep 20 '17 at 04:22

2 Answers2

0

What are you trying to achieve? generally speaking - No. Button, labels, or view loaded by a view controller are only in scope (loaded into memory) when the view controller's view is displayed.

Very rarely would you need to initialize the view controller and make a method call before the view is displayed, so the real question is why are you wanting to do this. Keep in mind its is called "view controller" i.e it controls the current views objects. I believe there is flaw in your design by wanting to do this.

  • I have a View on which I ask for user input.Then I show the second view which has a progress panel and stuff;This is where the real processing is performed. Currently I only have one `ViewController.Swift` File.If I can link the controls from the second view e.g.:progress bar I can run the code in ViewController.swift and update the second view.I don't know if thats possible.Please advice. – techno Sep 19 '17 at 17:36
  • How are you loading the second view with the progress panel. because If the second view is loaded by the viewController you should be able to reference it in the viewController. – oldmanswit Sep 19 '17 at 17:44
  • There is a single window controller and 2 view controllers.I link the first view controller to the second view controller using a segue on button press. – techno Sep 19 '17 at 17:46
  • To properly communicate between the two view controllers you need two parts. – oldmanswit Sep 19 '17 at 17:53
0

Making the assumption that the first view controller is not destroy when loading the second (i.e the second is a popup):

To properly communicate between the two view controllers you need two parts.

When performing the segue you need set parameter being passed to the second view controller. https://developer.apple.com/documentation/appkit/nssegueperforming Inorder to communicate back to first view controller you need to implement a delegate. I.e the second view controller delegates work back to the first: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html - view the section on delegation.

  • Alternatively you programmatically load the progress view inside the first view controller by putting in a new view (not a new view controller's view). Then the progress panel is in scope of the first view controller. – oldmanswit Sep 19 '17 at 18:06
  • By View do you mean a `Box` ? – techno Sep 19 '17 at 18:14