Custom UIStoryboard using Swift

你说的曾经没有我的故事 提交于 2019-12-24 10:36:00

问题


Has anyone managed to create a custom UIStoryboard using Swift.

If you jump to the definition of UIStoryboard it only has one initialiser. However it seems to return a UIStoryboard.

init(name: String!, bundle storyboardBundleOrNil: NSBundle!) -> UIStoryboard

I though initialisers did not return a type. Anyway, when I attempt to call this super initialiser from my subclass initialiser I get the following error

Must call a designated initialiser of the superclass 'UIStoryboard'

Has anyone successfully done this. Is this a bug or am I doing it wrong?


回答1:


Use the class (type) method to instantiate the storyboard.

let storyboard : UIStoryboard = UIStoryboard(name: "storyboardName", bundle: nil);

Edit: You are not supposed to subclass UIStoryboard. There are no public designated initializers for this class. Instead you are supposed to use the class method to create your storyboard, as there is a lot of private Apple API code to load the storyboard from it's compiled *.storyboardc file. The actual loading of the storyboard must be done via the class method.

Subclassing wouldn't help since the class method is only guaranteed to return a UIStoryboard, and not your specific subclass. Try to use another design pattern to achieve your goals. You could add a category (Swift extension) to UIStoryboard to add whatever additional methods you want, and call those from your code.



来源:https://stackoverflow.com/questions/24271131/custom-uistoryboard-using-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!