我正在尝试为文档创建详细视图。详细视图的顶部是90像素高的卡片式布局,下面是在两个控制器之间切换的视图寻呼机:InfoViewController(显示一些元信息)和DocumentViewController(显示pdf)。
我在一个带有两个UIViews的xib文件中布置了顶级控制器(DetailViewController):viewForCard和viewForPager。但是,在调用viewDidLoad方法时,InfoViewController和DocumentViewController中定义的出口尚未初始化。任何人都知道为什么?
DetailViewController中的主要代码(容器视图viewForCard和viewForPager的约束已在xib文件中设置:
@IBOutlet weak var viewForCard: UIView!
@IBOutlet weak var viewForPager: UIView!
var pageViewController:UIPageViewController = UIPageViewController()
var infoVC:InfoViewController = InfoViewController()
var documentVC:DocumentViewController = DocumentViewController()
override func viewDidLoad() {
super.viewDidLoad()
pageViewController.delegate = self
pageViewController.dataSource = self
addChildViewController(pageViewController)
pageViewController.setViewControllers([infoVC], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
initLayout()
}
func initLayout(){
var cardView:UIView = Card(document: document).contentView
cardView.setTranslatesAutoresizingMaskIntoConstraints(false)
var pagerView = pageViewController.view
pagerView.setTranslatesAutoresizingMaskIntoConstraints(false)
viewForCard.addSubview(cardView)
viewForPager.addSubview(pagerView)
let bindings = ["card": cardView, "pager": pagerView]
viewForCard.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0.0-[card]-0.0-|", options: nil, metrics: nil, views: bindings))
viewForCard.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-0.0-[card]-0.0-|", options: nil, metrics: nil, views: bindings))
viewForPager.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0.0-[pager]-0.0-|", options: nil, metrics: nil, views: bindings))
viewForPager.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-0.0-[pager]-0.0-|", options: nil, metrics: nil, views: bindings))
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?{
if viewController == documentVC{
return infoVC
}else{
return nil
}
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?{
if viewController == infoVC{
return documentVC
}else{
return nil
}
}
答案 0 :(得分:1)
你应该改变:
var infoVC:InfoViewController = InfoViewController()
var documentVC:DocumentViewController = DocumentViewController()
要:
var infoVC:InfoViewController = InfoViewController(nibName: "InfoViewController", bundle: nil)
var documentVC:DocumentViewController = DocumentViewController(nibName: "DocumentViewController", bundle: nil)
答案 1 :(得分:0)
显然视图控制器是用nibname nil初始化的,这就是没有设置插座的原因。
答案 2 :(得分:0)
在Swift 5中
像这样初始化viewController。
let firstVC:FirstVC = storyboard?.instantiateViewController(withIdentifier: "FirstVC") as! FirstVC
pageController.setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)