2

I've got a layout in my app that stays the same besides the images which will differ depending on what button is pressed in the previous view. What I was thinking to do is set it to that if btn1 is pressed the user it taken to the next view but then VCfile1.swift is used and if the user presses btn2 then he is taken to the next view and VCFile2.swift is used for the same viewcontroller created in the storyboard. How could I achieve this? (I'm not sure I this is the correct method but if not please do advise)

Swift 3, Xcode 8

Henry
  • 339
  • 2
  • 4
  • 14
  • Copy the viewcontroller created in the storyboard. Set ones class to `VCfile1`, the other to `VCfile2`. Segue to the desired one according to which button is pressed. Or: Create a XIB (view) instad of storyboard and load this xib in both VCs programatically. – shallowThought Dec 15 '16 at 20:23
  • I can't create multiple Viewcontrollers cause then they'd be like 100 viewcontrollers @shallowThought – Henry Dec 16 '16 at 19:38
  • Guess option 2 is for you then (XIB...). – shallowThought Dec 16 '16 at 19:40
  • Haven't really used XIB views (newbie to coding). Can you have a variable that shared amoungest the view controllers? So like if btn1 is pressed in ViewController1 then var xyz = "btn1pressed" then I could say in the next viewcontroller (where the user is taken to when btn1 is pressed) if var xyz = "btn1pressed" do whatever and if it's = to btn2pressed then do something else. Can something like this be done? @shallowThought – Henry Dec 16 '16 at 19:54
  • What you want, how I understand, is to use the same `View` in different `ViewController(.swift)` classes. So you can create a `XIB` as your view (re-)use it in your `ViewController`s programmatically. If you have never heard of it, you have to learn it. Google for tutorials. – shallowThought Dec 16 '16 at 20:06
  • 1
    Off topic: I think your design is wrong, if you need 100 ViewController, all with the same view btw. – shallowThought Dec 16 '16 at 20:08
  • Can you post your answer as an answer not a commen so I can mark it as accepted @shallowThought – Henry Dec 17 '16 at 12:23

1 Answers1

1

First: If you need 100 UViewControllers, all with the same view, your doing something wrong.

To re-use the same View in different ViewController(.swift) classes you may want tocreate your UIView as a XIB and (re-)use it in your ViewControllers programmatically.

  • Create a class for your view:

    class MyView: UIView { // }

  • Create a XIB file (new -> file -> View) MyView.xib, set its class to MyView and customize it to your needs.

  • Create an instance and use it in your UIViewControllers:

    class MyViewController: UIViewController {
        var myView: MyView?
    
        override func viewDidLoad() {
            super.viewDidLoad()
            myView = UINib(nibName: "MyView", bundle: nil).instantiate(withOwner: self, options: nil).first as? MyView
            view.addSubview(myView!)
            // You might want to set constraints here
        }
    }
    
shallowThought
  • 19,212
  • 9
  • 65
  • 112
  • I created the Xib file and have one class connected to it. I'm having trouble connecting other classes to it though. I added another class and used `class TestAd1: UIViewController { var view = TestAd1.instanceFromNib self.view.addSubview(view()) class func instanceFromNib() -> { return UINib(nibName: "AdSpaceShared.xib", bundle: nil).instance(withOwner: nil, options: nil)[0] as! UIView } ` – Henry Dec 17 '16 at 19:59
  • Updated answer. Hopefully helps to get the idea. – shallowThought Dec 18 '16 at 14:17