【发布时间】:2015-10-24 15:50:42
【问题描述】:
至少在 iOS 8 或更高版本上,我无法找到有关使用 swift 执行 segue 或某种视图转换的帮助。除了实现从主视图控制器到 xib 的转场之外,我试图尽可能避免使用情节提要编辑器。
这是我目前所拥有的:
let bundle = NSBundle(forClass: self.dynamicType)
let secondViewController = ViewController(nibName: "SwitchRegionSegue", bundle: nil)
self.presentViewController(secondViewController, animated: true, completion: nil)
上面的代码是从主 ViewController.swift 使用 Single View Application 新项目脚手架调用的。但它会导致崩溃:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "SwitchRegionSegue" nib but the view outlet was not set.'
我也有这两个文件:
SwitchRegionSegue.xib
SwitchRegionSegue.swift
我已确保将 xib 的文件所有者自定义类设置为 SwitchRegionSegue : UIView
我的 SwitchRegionSegue.swift 只是一个空白画布,看起来像这样:
import UIKit
class SwitchRegionSegue: UIView {
var view: UIView!
var nibName = "SwitchRegionSegue"
func xibSetup() {
view = loadViewFromNib()
// use bounds not frame or it'll be offset
view.frame = bounds
// Make the view stretch with containing view
view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
addSubview(view)
}
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: nibName, bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
}
}
有人知道我在这里做的不对吗? (推荐一本好的 swift & xib 教科书会很棒)
【问题讨论】:
标签: ios swift uiviewcontroller segue