我正在尝试将xib文件作为子视图放在我的主viewController中,我收到一个非常奇怪的错误消息;
当我有这段代码时:
class MyViewController: UIInputViewController {
var mainView : UIView!
override func viewDidLoad() {
super.viewDidLoad()
mainView = UINib(nibName: "MainView", bundle: nil).instantiateWithOwner(self, options: nil)[0] as! UIView
self.view.addSubview(mainView)
}
}
我有以下错误消息:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview' ***
但是当我有这段代码时:
class MyViewController: UIInputViewController {
override func viewDidLoad() {
super.viewDidLoad()
let subViewsArray = [
UINib(nibName: "MainView", bundle: nil).instantiateWithOwner(self, options: nil)[0] as! UIView
]
self.view.addSubview(subViewsArray[0])
}
}
或只是这一个:
class MyViewController: UIInputViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(UINib(nibName: "mainView", bundle: nil).instantiateWithOwner(self, options: nil)[0] as UIView)
}
}
它正在工作......那么,为什么第一个代码无效!?
修改:
当我试图放置两个子视图时:
class MyViewController: UIInputViewController {
override func viewDidLoad() {
super.viewDidLoad()
let subViewsArray = [
UINib(nibName: "MainView", bundle: nil).instantiateWithOwner(self, options: nil)[0] as! UIView,
UINib(nibName: "AnotherView", bundle: nil).instantiateWithOwner(self, options: nil)[0] as! UIView
]
self.view.addSubview(subViewsArray[0])
self.view.addSubview(subViewsArray[1])
}
}
它不再使用相同的错误消息:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview' ***
答案 0 :(得分:-1)
嗨,在我的情况下,这个崩溃的xib文件是UIViewController类。 我更改xib文件FileOwners删除视图连接 并为我工作