0

I'm new to Swift, so I apologize for my lack of knowledge of almost anything related to Swift.

Right now, I'm developing an app that uses the ARSlidingPanel library, so now there are two ViewControllers present on the screen (Main view, with the dark background, and Panel view, which is the green background). I want to be able to set vars/call functions from the Panel view to the Main view. For example if I click a button in the panel view, I want to display some text in the main view. Right now I've started with using static vars but from my knowledge of Java/Android, I know this is probably not the right way to go. Any tips for a cleaner/more acceptable way of doing things?

enter image description here

Jason John
  • 934
  • 2
  • 12
  • 23
  • I see 2 solutions, the simplest is to store a weak reference of your main VC in your second VC (`weak var mainVC:myVCClass!` in your second VC class), just set it when you create the second VC or call the segue. Another solution is to use a delegate design pattern, which is more sophisticated. – Axel Guilmin Mar 23 '16 at 01:58
  • Can you explain the delegate design pattern a little further in this use case? – Jason John Mar 23 '16 at 02:04
  • 1
    Define a protocol like `protocol MainVCDelegate { func myButtontapped(sender:UIButton) }` and store a reference to the delegate in your MainVC `var delegate:MainVCDelegate?` and call it when it is needed, like when the button is tapped `self.delegate?.myButtontapped(sender)`. The secondVC should inherit it `class SecondVC : UIViewController, MainVCDelegate` and change the view within the delegate func implementations, don't forget to set `myMainVC.delegate = mySecondVC` when creating it and you should be good to go. – Axel Guilmin Mar 23 '16 at 02:22
  • It almost makes sense. You had me up until "don't forget to set myMainVC.delegate = mySecondVC when creating it and you should be good to go." Where do I set this up? – Jason John Mar 23 '16 at 02:40
  • I don't know about ARSlidingPanel library, but basically right after you instanciate the second VC (could also be in `prepareForSegue:` if you use it) You can check Daniel Ormeño link about the delegate – Axel Guilmin Mar 23 '16 at 02:59
  • @AxelGuilmin Again, sorry for being such a noob. When you say "instantiate" do you mean when the ViewController has loaded (viewDidLoad?). I don't have a reference to the other view controller so I can't call self.delegate = mySecondVC because I don't have a 'mySecondVC' var – Jason John Mar 23 '16 at 23:40
  • It depends on your implementation and `ARSlidingPanel`. Seeing the `ARSlindingPanel` readme you can either use the storyboard and segues or create it by code (with "new"). You can check this response http://stackoverflow.com/a/7865100/1327557 about using the `prepareForSegue:` method to find a reference to your second VC if you use the storyboard. – Axel Guilmin Mar 24 '16 at 07:51

1 Answers1

1

There are a few different options here. However, the first thing that comes up to mind is if you really need to use two independent view controllers for the same view?.

  • One solution would be to use delegates. Write a swift protocol that has has the methods and functionality you want to expose, for example setting up values in the view controller's outlets, then implement the protocol in one view controller. In your other view controller, you can then create a delegate variable of type UIViewController -let delegate: UIViewController - and use that instance of your delegate to modify the view. Here's a guide on this
  • Even better, you can add view controllers as sub view controllers of your view, apple provides a good bit of documentation on this, you can find it here.

Hope this helps.

Daniel Ormeño
  • 2,743
  • 2
  • 25
  • 30