使用 Swift 自定义 UIStoryboard [英] Custom UIStoryboard using Swift

查看:40
本文介绍了使用 Swift 自定义 UIStoryboard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人设法使用 Swift 创建自定义 UIStoryboard.

Has anyone managed to create a custom UIStoryboard using Swift.

如果你跳转到 UIStoryboard 的定义,它只有一个初始化器.但是它似乎返回了一个 UIStoryboard.

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

必须调用超类UIStoryboard"的指定初始化器

有没有人成功做到这一点.这是一个错误还是我做错了?

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

推荐答案

使用类(类型)方法来实例化故事板.

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

让故事板:UIStoryboard = UIStoryboard(name: "storyboardName", bundle: nil);

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

你不应该继承 UIStoryboard.此类没有公共指定的初始值设定项.相反,你应该使用类方法来创建你的故事板,因为有很多私有的 Apple API 代码可以从它编译的 *.storyboardc 文件中加载故事板.故事板的实际加载必须通过类方法完成.

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.

子类化无济于事,因为类方法只能保证返回 UIStoryboard,而不是您的特定子类.尝试使用另一种设计模式来实现您的目标.您可以向 UIStoryboard 添加类别(Swift 扩展)以添加您想要的任何其他方法,并从您的代码中调用这些方法.

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.

这篇关于使用 Swift 自定义 UIStoryboard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆