Link 带有 UIViewController 和自定义的 xib 文件 Class
Link xib file with UIViewController and Custom Class
为了创建 xib 文件,我添加了一个新的 cocoa touch 文件子 class 的 UIViewController,并检查了 "Also Create XIB file"。
我的 xib 文件中只有一个标签。
在我的视图控制器中:
import UIKit
class TestControllerViewController: UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
除了我的 IBoutlet(我至少检查了连接检查器 10 次)。
我的习惯 class :
import Foundation
import UIKit
class Card: UIView {
internal var number:Int?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
convenience init()
{
self.init()
}
}
extension UIView {
class func loadFromNibNamed(nibNamed: String, bundle : NSBundle? = nil) -> UIView? {
return UINib(
nibName: nibNamed,
bundle: bundle
).instantiateWithOwner(nil, options: nil)[0] as? UIView
}
}
我在初始 viewController 中实例化了这个 xib 文件:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var test:Card = Card.loadFromNibNamed("TestControllerViewController")! as! Card
test.number = 18
//self.view.addSubview(test)
}
我仍然有这个错误:
2015-07-27 17:48:52.919 TestXib[6865:578635] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x7fcec14b1970> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label.'
我不知道为什么,我尝试清理/重建、删除 DerivedData、重新创建 xib 文件等。
你能帮帮我吗?
谢谢。
本
你处理的方式不对。来自另一个 UIViewController
的 xib 的自定义 UIView
会造成混淆。但我会带你摆脱这个错误。请参阅下面的设置。
将 FileOwner
从 TestControllerViewController
更改为 Card
删除 Outlet
引用:
将主视图更改为 Card
。它可以帮助您将 UIView
转换为 Card
。
顺便说一句:我建议您创建新的 xib 文件 Card.xib
并将其与 Card.swift
连接
为了创建 xib 文件,我添加了一个新的 cocoa touch 文件子 class 的 UIViewController,并检查了 "Also Create XIB file"。
我的 xib 文件中只有一个标签。
在我的视图控制器中:
import UIKit
class TestControllerViewController: UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
除了我的 IBoutlet(我至少检查了连接检查器 10 次)。
我的习惯 class :
import Foundation
import UIKit
class Card: UIView {
internal var number:Int?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
convenience init()
{
self.init()
}
}
extension UIView {
class func loadFromNibNamed(nibNamed: String, bundle : NSBundle? = nil) -> UIView? {
return UINib(
nibName: nibNamed,
bundle: bundle
).instantiateWithOwner(nil, options: nil)[0] as? UIView
}
}
我在初始 viewController 中实例化了这个 xib 文件:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var test:Card = Card.loadFromNibNamed("TestControllerViewController")! as! Card
test.number = 18
//self.view.addSubview(test)
}
我仍然有这个错误:
2015-07-27 17:48:52.919 TestXib[6865:578635] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x7fcec14b1970> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label.'
我不知道为什么,我尝试清理/重建、删除 DerivedData、重新创建 xib 文件等。
你能帮帮我吗?
谢谢。 本
你处理的方式不对。来自另一个 UIViewController
的 xib 的自定义 UIView
会造成混淆。但我会带你摆脱这个错误。请参阅下面的设置。
将 FileOwner
从 TestControllerViewController
更改为 Card
Outlet
引用:
将主视图更改为 Card
。它可以帮助您将 UIView
转换为 Card
。
顺便说一句:我建议您创建新的 xib 文件 Card.xib
并将其与 Card.swift