0

I am neither an iOS developer, nor a swift developer, but please bear with me:

I am currently trying to implement a simple iOS app but I have difficulties understanding how exactly I am supposed to set up custom UIViews and ViewControllers for those UIViews.

I am using a UIScrollView that is containing items a little bit more complex than just images, thats what I use custom views for. What I did was:

  • I created a .xib file, the view itself. I added some elements (here it is only a textfield, for simplicity's sake).
  • I created a cocoa touch class "CustomView" that inherits from UIView and set my view up to be of that class (inside the class I just set up elements and such).

created the view and the corresponding "CustomView" class

view is now of type "CustomView"

Now I want a ViewController that controls the class whenever it is rendered (for example reacting to the changing textField). I cant manage everything from my main ViewController, because it would get too big (e.g. 3 scrollViews * 5 subviews that need to be managed). I want a solution that uses ViewControllers for each subview (in case they themselves will have subviews, too).

How do I do that?

Do I need to add some sort of childViewController?

I really am at loss, most of the blog posts and SO examples simply do not work and/or are outdated and I am unsure about whether or not I got the whole View - ViewController pattern wrong.

maxwell
  • 3,788
  • 6
  • 26
  • 40
  • 1
    As I understand you want to insert many viewcontrollers with scrollview inside into another scrollview, am I right? – kkiermasz Apr 20 '19 at 08:57
  • I tried to make the question as generic as possible, but that, for example, is very similar to the thing I am trying to do. In fact, I have a scrollView with items in it that are a bit more complex and therefor need seperate Controllers of some sort. I _do_ have a Scrollview containing items that controll scrollviews that containg a custom view. It's not only that I don't know how to handle my specific problem, I just know too few things about MVC and swift to come up with a general, applicable pattern for these situations. – Thomas Pötzsch Apr 20 '19 at 09:01
  • https://supereasyapps.com/blog/2014/12/15/create-an-ibdesignable-uiview-subclass-with-code-from-an-xib-file-in-xcode-6 Check this link, it can be helpful for you – IBAction Apr 20 '19 at 09:06
  • Thank you for the link, it seems like a greate resource, but he does not build a ViewController for his custom View. I have seen people put methods into their `CustomView` classes that react to events, but imho this is not valid MVC since the view then also controls (models should know nothing of views and vice versa). As I said, none of the resources I found online helped me, but maybe I am trying to implement something I shouldn't be trying to implement... – Thomas Pötzsch Apr 20 '19 at 09:11

1 Answers1

0

Let's say you have two view controllers, MainViewController and TableViewController. TableVC's main view is to be a subview of MainVC's main view. In addition, you wish to pass back to MainVC which cell was selected in TableVC.

A solution is (a) make TableVC be a child to MainVC and (b) make MainVC be a delegate for TableVC.

TableViewController:

protocol TableVCDelegate {
    func cellSelected(sender: TableViewController)
}

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    // please note that you can do delegation differently,
    // this way results in crashes if delegate is nil!

    var delegate:TableVCDelegate! = nil
    var someValue = ""

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        // set someValue to contents in the selected cell or it's data source

        someValue = "Hello World!"
        delegate.cellSelected(sender: self)

    }
}

MainViewController:

class MainViewController: UIViewController, TableVCDelegate {

    let tableVC = TableViewController()

    override func viewDidLoad() {

        // make tableVC be a child of this VC

        addChild(tableVC)
        tableVC.didMove(toParent: self)
        tableVC.delegate = self

        // position tableVC.view

        tableVC.view.translatesAutoresizingMaskIntoConstraints = false

    }

    func cellSelected(sender: TableViewController) {
        print(sender.someValue)  // this should send "Hello World!" to the console
    }
}

This is obviously untested code, but it is based on product code. This is meant to be a shell to help you get started.