4

After Implementing the following (Class Inheritance):

class UIViewControllerA: UIViewControllerB {
}

How to let UIViewControllerA to inherits UIViewControllerB IBOutlets? How can I connect the components at Storyboard to the subclass UIViewControllerA?

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
keen
  • 43
  • 1
  • 4
  • Inheritance is a programming language feature, it is not related to the storyboard, what's exactly the problem? – Ahmad F Mar 13 '17 at 07:06
  • when I use self. label in UIViewControllerA, it's nil , all controls are nil – keen Mar 13 '17 at 07:10
  • By default if you are implement them as IBOutlets, they should be `nil` by default, unless you connect them from storyboard. – Ahmad F Mar 13 '17 at 07:12
  • Is there more convenient way?I did implement them as IBOutlets – keen Mar 13 '17 at 07:20
  • Just to make sure that I understood what are you trying to achieve, you want to declare IBOutlets in the base class and let the sub class to interact with them via storyboard, right? – Ahmad F Mar 13 '17 at 07:29
  • right,in superClass has a webview, and it's delegate has more js monitor,i need use the webview in subclass – keen Mar 13 '17 at 07:33
  • Maybe you can *insert* the UIViewControllerB `view` in UIViewControllerA `view` Take a look at `insertSubview` method on `UIViewController` class – Adolfo Mar 13 '17 at 07:39
  • thanks,I use same IBOutlets in UIViewControllerB and connect them to UIViewControllerA – keen Mar 13 '17 at 08:15

1 Answers1

7

If your goal is to let IBOutlets to be inherited, you should do the following:

1- Add the IBOutlets in the Super Class:

Super class (UIViewController) should not be directly connected to any ViewController at the storyboard, it should be generic. When adding the IBOutlets to the super class, they should not be connected to any component, sub classes should do that. Also, you might want to do some work (that's why you should apply this mechanism) for the IBOutlets in the super class.

Super Class should be similar to:

class SuperViewController: UIViewController, UITableViewDataSource {
    //MARK:- IBOutlets
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // setting text to the label
        label.text = "Hello"

        // conforming to table view data source
        tableView.dataSource = self
    }

    // handling the data source for the tableView
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

        cell?.textLabel?.text = "Hello!"

        return cell!
    }
}

2- Inherit Super Class and Connect the IBOutlets:

Simply, your sub class should be similar to:

class ViewController: SuperViewController {
    override func viewDidLoad() {
        // calling the super class version
        super.viewDidLoad()
    }
}

From storyboard, assign the view controller to ViewController (Sub Class), then rebuild the project (cmd + b).

Now, after selecting the desired View Controller and selecting "Connection Inspector", you should see -in the IBOutlets section-:

enter image description here

you can manually connect them to the UI components that exists in your sub class ViewController (drag from the empty circle to the component). they should look like:

enter image description here

And that's it! Your sub class's table view and label should inherit what's included in the super class.

Hope this helped.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • Thank you for your answer carefully。I have solved yesterday from your answer:By default if you are implement them as IBOutlets, they should be nil by default, unless you connect them from storyboard. – keen Mar 14 '17 at 01:32
  • 1
    Glad to help :) Yes, IBOutlets are nil by default, unless you connect them from storyboard. – Ahmad F Mar 14 '17 at 07:23