Swift Must call a designated initializer of the superclass uiinputviewcontroller
Asked Answered
I

1

8

I get the error in the subject after this morning's upgrade to 8.3.

The code below used to work perfectly, however it doesn't compile anymore. Can any of you please help me?

protocol CustomAccessoryProtocol {
    func controlButtonPressed(tag:Int)
}

class CustomAccessory : UIInputViewController {
    var accessoryView : UIView!
    var delegate : CustomAccessoryProtocol!

    @IBOutlet weak var returnButton: UIButton!
    @IBOutlet weak var backButton: UIButton!
    @IBOutlet weak var forwardButton: UIButton!

    init(delegate: CustomAccessoryProtocol){
        super.init()
        self.delegate = delegate
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")
    }

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        let customNib = UINib(nibName: "CustomAccessory", bundle: nil)
        accessoryView = customNib.instantiateWithOwner(self, options: nil)[0] as! UIView
    }

    @IBAction func buttonPress(sender: AnyObject) {
        delegate.controlButtonPressed(sender.tag!)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(accessoryView)
    }
}
Incoordination answered 9/4, 2015 at 6:17 Comment(4)
I wonder what the designated initializer for UIInputViewController? Not init(frame: CGRect) I presume. ?Orfurd
Isn't the first init a convenience init? Do you have to add convenience keyword to it? I don't have it down to a science. I've had some cases where I had to struggle with it for awhile to figure it out. Based on the initializers you do have, I assume you read the Swift documentation on the subject at some point...Orfurd
Did anything change in the 8.3 docs for UIInputViewController or UIViewController?Orfurd
I can't find anything changed or maybe I'm missing something, I am also wondering about the designated initializer as all I can see are the ones that I already have. Maybe they will update the documentation as so far I cant see anything on this. Hoewever, it seems to work when adding convenience to the first init and self.init rather than super. You sir just made my day!Incoordination
N
7

I had the same problem on the following code with NSWindowController:

init() {
    super.init()
}

I changed it to:

convenience init() {
    self.init()
}

I'm thinking that Apple is enforcing convenience inits more strictly than before.

Noseband answered 14/4, 2015 at 8:28 Comment(5)
with self.init() I get warning "Could not find an overload for 'init' that accepts the supplied argumentsStonefish
Adding convenience init() is giving me a bad excess. Please help.Armanda
Maybe because you're referencing self.init()which references itself?Jeannettajeannette
No. Its like recursive function call, it never works. Why its having this much upvotes?Oehsen
This causes an infinite loop.Damper

© 2022 - 2024 — McMap. All rights reserved.