我正在尝试使用代码加载包含来自笔尖的UITableView的视图:
class HintTable:UIView, UITableViewDataSource, UITableViewDelegate{
var searchResults = Array<String>()
let delegate:MovableAnnotationDelegate
@IBOutlet var myTableView:UITableView!
init(frame: CGRect, delegate:MovableAnnotationDelegate) {
self.delegate=delegate
super.init(frame: frame)
loadViewFromNib ()
}
required init?(coder aDecoder: NSCoder) {
self.delegate=InArrivoHDViewController.sharedDetailController()
super.init(coder: aDecoder)
loadViewFromNib ()
}
func loadViewFromNib() {
NSBundle.mainBundle().loadNibNamed("HintTableView", owner: self, options: nil)
self.myTableView.backgroundColor=UIColor(red:0.2890625, green:0.75390625, blue:0.3046875, alpha:0.5)
self.backgroundColor=UIColor(red:0.2890625, green:0.75390625, blue:0.3046875, alpha:0.5)
print("myTableView %@", self.myTableView);
self.myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
}
然而它在命令中没有任何暗示而崩溃:
NSBundle.mainBundle().loadNibNamed("HintTableView", owner: self, options: nil)
在Xib中,我将类设置为File的所有者,而不是在身份检查器中获取它。我也很不清楚如何访问tableView,因为IBOutlet似乎没有连线。
这是一个崩溃日志:
Last Exception Backtrace:0 CoreFoundation
0x182b3cf48 exceptionPreprocess + 124 1 libobjc.A.dylib
0x197fe7f80 objc_exception_throw + 56 2 CoreFoundation
0x182b3cc08 - [NSException raise] + 12 3基础
0x1839b8014 - [NSObject(NSKeyValueCoding)setValue:forKey:] + 268 4
UIKit 0x1883e3a4c - [UIView(CALayerDelegate) setValue:forKey:] + 184 5 UIKit 0x188596794 - [UIRuntimeOutletConnection connect] + 124 6 CoreFoundation 0x182a669cc - [NSArray makeObjectsPerformSelector:] + 232 7 UIKit
0x18859517c - [UINib instantiateWithOwner:options:] + 1748 8 UIKit
0x188597408 - [NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 224 9 inArrivoHD
0x10023218c 0x100074000 + 1827212 10 inArrivoHD
0x100231bf8 0x100074000 + 1825784 11 inArrivoHD
0x100231d2c 0x100074000 + 1826092 12 inArrivoHD
0x10024ec34 0x100074000 + 1944628 13 inArrivoHD
0x100250678 0x100074000 + 1951352 14 UIKit
0x1880d0098 - [UIViewController loadViewIfRequired] + 996 15 UIKit
0x18818f580 - [UINavigationController _layoutViewController:] + 72 16 UIKit 0x18818f458 - [UINavigationController _updateScrollViewFromViewController:toViewController:] + 416 17 UIKit 0x18818e6a0 - [UINavigationController _startTransition:fromViewController:toViewController:] + 144 18 UIKit 0x18818e244 - [UINavigationController _startDeferredTransitionIfNeeded:] + 868 19 UIKit 0x18818de6c - [UINavigationController __viewWillLayoutSubviews] + 60 20 UIKit 0x18818ddd4 - [UILayoutContainerView layoutSubviews] + 208 21 UIKit 0x1880cb7ac - [UIView(CALayerDelegate)layoutSublayersOfLayer:] + 644 22 QuartzCore 0x1878cab58 - [CALayer layoutSublayers] + 148 23 QuartzCore 0x1878c5764 CA :: Layer :: layout_if_needed(CA :: Transaction *)+ 292 24 QuartzCore
0x1878c5624 CA :: Layer :: layout_and_display_if_needed(CA :: Transaction *) + 32 25 QuartzCore 0x1878c4cc0 CA :: Context :: commit_transaction(CA :: Transaction *)+ 252 26 QuartzCore 0x1878c4a08 CA :: Transaction :: commit()+ 512 27 QuartzCore
0x1878be0f8 CA :: Transaction :: observer_callback(__ CFRunLoopObserver *, unsigned long,void *)+ 80 28 CoreFoundation
0x182af3bd0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 32 29 CoreFoundation 0x182af1974 __CFRunLoopDoObservers + 372 30 CoreFoundation 0x182a20cc0 CFRunLoopRunSpecific + 416 31 UIKit
0x18813e1c8 - [UIApplication _run] + 460 32 UIKit
0x188138ffc UIApplicationMain + 204 33 inArrivoHD
0x1002cc3c0 0x100074000 + 2458560 34 libdyld.dylib
0x19882a8b8 start + 4
答案 0 :(得分:-1)
我最终在Storyboard的主ViewController中插入视图,并将委托和源适当地分配给View。