storyboard加载viewcontroller protocol实现
来源:互联网 发布:听歌识曲的软件 编辑:程序博客网 时间:2024/04/27 13:33
@objc(ABLoadFromStoryboardProtocol)
protocol ABLoadFromStoryboardProtocol: class {
static func loadFromStoryboard() -> UIViewController
}
extension UIViewController {
static func loadFromStoryboard<T: ABLoadFromStoryboardProtocol>(_: T.Type) -> T {
if let vc = T.loadFromStoryboard() as? T {
return vc
}
fatalError("Could not instantiateViewController from storyboard")
}
protocol ABLoadFromStoryboardProtocol: class {
static func loadFromStoryboard() -> UIViewController
}
extension UIViewController {
static func loadFromStoryboard<T: ABLoadFromStoryboardProtocol>(_: T.Type) -> T {
if let vc = T.loadFromStoryboard() as? T {
return vc
}
fatalError("Could not instantiateViewController from storyboard")
}
}
需要加载的viewcontroller需要实现ABLoadFromStoryboardProtocol协议
extension CustomViewController: ABLoadFromStoryboardProtocol {
static func loadFromStoryboard() -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "custom_view_story_identifier")
}
}
加载调用方法
let customVC = UIViewController.loadFromStoryboard(CustomViewController.self)
现在这种方式就不需要每次找到identifier才能加载和强制转换为指定的viewcontroller了
0 0
- storyboard加载viewcontroller protocol实现
- 从XIB、Storyboard加载View或ViewController
- iOS-代码加载storyboard中的ViewController
- iOS-代码加载storyboard中的ViewController
- 如何在自定义的ViewController加载Storyboard里面的ViewController
- Storyboard中ViewController加载的四种方式
- 不依赖storyboard实现ViewController跳转和传参
- StoryBoard中使用Segue实现ViewController之间传值
- 不依赖storyboard实现ViewController跳转和传参
- ViewController Graph By StoryBoard
- Storyboard 如何初始化 ViewController
- 使用协议Protocol实现ViewController之间传值
- 如何装载Storyboard中的ViewController?
- [IOS]装载Storyboard中的ViewController
- 代码装载storyboard中的viewcontroller
- storyboard中游离的viewcontroller
- 通过storyboard id获得viewcontroller
- 在Storyboard中获取Viewcontroller
- 文章标题
- spring cloud: Hystrix断路器(熔断器)
- 打开office 2007中的Word文档时出现"The setup controller has encountered a problem during install
- 感谢关注过或支持过这个框架的所有人
- 对类进行封装
- storyboard加载viewcontroller protocol实现
- Http请求中,post和put的区别
- JAVA中三大特性之继承
- 谷歌要抛弃Android拥抱Andromeda
- 从 MVC 到前后端分离
- Android ImageView 正确使用姿势
- MyEclipse 快捷键1
- 冒泡排序
- 关于调用线程无法访问此对象,因为另一个线程拥有该对象的问题的解决办法