Loading ViewController from xib file(从 xib 文件加载 ViewController)
问题描述
我有一个 MyViewController.swift
和一个 MyViewController.xib
展示 MyViewController 的布局.
I had a MyViewController.swift
and a MyViewController.xib
presenting the layout of MyViewController.
我尝试了不同的方法来加载这个视图控制器,包括:
I tried different methods to load this view controller including:
//1
let myVC = UINib(nibName: "MyViewController", bundle:
nil).instantiateWithOwner(nil, options: nil)[0] as? MyViewController
//2
let myVC = NSBundle.mainBundle().loadNibNamed("MyViewController", owner: self, options: nil)[0] as? MyViewController
//3
let myVC = MyViewController(nibName: "MyViewController", bundle: nil)
第三个是唯一成功的初始化,但前两个导致错误:
The third one is the only successful initialisation, but the previous two are causing error:
由于未捕获的异常NSUnknownKeyException"而终止应用程序,
Terminating app due to uncaught exception 'NSUnknownKeyException',
原因:'[setValue:forUndefinedKey:]: this类与键 XXX 的键值编码不兼容.
reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key XXX.
这些加载方法有什么问题?
What's wrong with those loading methods?
推荐答案
Swift 3
let myViewController = MyViewController(nibName: "MyViewController", bundle: nil)
self.present(myViewController, animated: true, completion: nil)
或推入导航控制器
self.navigationController!.pushViewController(MyViewController(nibName: "MyViewController", bundle: nil), animated: true)
这篇关于从 xib 文件加载 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 xib 文件加载 ViewController
- URL编码Swift iOS 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- UITextView 内容插图 2022-01-01