Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 - override initializer for UINavigationController to set rootviewcontroller

I'm using Swift 3 to override an init method that initializes my Navigation Controller with a rootviewcontroller (and set the rootviewcontroller delegate to self). But, I'm getting the following error:

Incorrect argument label in call (have 'rootViewController:', expected 'coder:')

class NavigationController: UINavigationController, RootViewControllerDelegate {

    let rvc = RootViewController()

    convenience init() {
        self.init(rootViewController: rvc) // Incorrect argument label in call (have 'rootViewController:', expected 'coder:')
        self.rvc.delegate = self
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

Can somebody explain what I'm doing wrong please? I initially tried using

override func init()

but Xcode ultimately had me convert that to

convenience init()

like image 434
Vee Avatar asked Feb 04 '23 22:02

Vee


1 Answers

The init(rootViewController:) is defined in UINavigationController, which is the super class of your NavigationController class. Therefore, you should use super instead of self to refer to it:

init() {
    super.init(rootViewController: rvc)
    self.rvc.delegate = self
}

Since you have one other initializer defined in NavigationController, Xcode thinks that you were trying to call that initializer. That's why it tells you to put coder: as the argument label.

like image 145
Sweeper Avatar answered Feb 07 '23 13:02

Sweeper