viewController custom init method with storyboard

前端 未结 2 443
-上瘾入骨i
-上瘾入骨i 2021-01-30 08:28

im having trouble overriding the initialization method for my CustomViewController thats designed in my Storyboard.

now im doing (in my mainViewController):



        
相关标签:
2条回答
  • 2021-01-30 09:03

    As another "workaround" for this problem, you could use delegation. Create a protocol that would act as data source for your UIViewController subclass from storyboard. Conform to this data source protocol, implement it and use it right after loading your UIViewController subclass:

    required init?(coder aDecoder: NSCoder)
        {
            super.init(coder: aDecoder)
    
            //invoke your data source here
        }
    

    I know... I also don't like this but... ;)

    0 讨论(0)
  • 2021-01-30 09:15

    The designated initializer for view controllers in storyboards is -initWithCoder:. Since most view controllers from a storyboard are created via segues, you usually see state set during -prepareForSegue:sender:.

    If you're instantiating a view controller directly from the storyboard like in the example you provided, then the pattern you've selected is appropriate.

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