How to write init methods of a UIViewController in Swift

前端 未结 8 1551
长发绾君心
长发绾君心 2020-12-07 17:14

I am unable to add init method to the following UIViewController class. I need to write some code in the init method. Do i have to write init(coder) method? Even when I add

相关标签:
8条回答
  • 2020-12-07 18:11

    Correct flow is, call the designated initializer which in this case is the init with nibName,

    init(tap: UITapGestureRecognizer)
    {
        // Initialise the variables
        tap = UITapGestureRecognizer(target: self, action: Selector(("handleTap:")))
    
        // Call the designated init of ViewController
        super.init(nibName: nil, bundle: nil)
    
        // Call your viewcontroller custom methods here
        setupViewControllers()
    }
    
    0 讨论(0)
  • I used:

    convenience init() {
        self.init(nibName:nil, bundle:nil)
    }
    

    Some people suggested:

    convenience init(){
        self.init()
    }
    

    But this gives you an infinite loop.

    0 讨论(0)
提交回复
热议问题