在显示具有清晰背景的当前图像后无法执行到另一个 ViewController

Cannot perform segue to another ViewController after showing the present one with a clear background

我跟随 Ray Wenderlich tutorial 了解如何创建 iOS 书籍动画,现在我正在尝试对其进行一些更改。

原始项目Navigation ControllerBooks View Controller组成 - 显示一系列书籍,通过点击一本书可以打开它 - 和Book View Controller - 显示所选书籍已打开。

我添加的内容:一个View Controller并设置为初始VC;一个 UIButtonNavigation Controller.

进行表演转场

我想在 Books View Controller 出现后在背景中显示 View Controller 但显然 iOS 从视图层次结构中删除了它下面的视图控制器,如果我设置这会导致黑色背景clearColor 在属性检查器中。然后我将以下代码添加到 ViewController.swift 以获得透明背景。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let vc = self.storyboard!.instantiateViewControllerWithIdentifier("BooksViewController") as! BooksViewController
    vc.view.backgroundColor = UIColor.clearColor()
    vc.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
    self.presentViewController(vc, animated: true, completion: nil)
}

它运行良好,我可以在看到书籍数组时在背景中看到初始 VC,但我无法再通过单击书籍来执行到 Book View Controller 的转换。所以显然 BooksViewController 中的 openBook 从未被调用过:

func selectedCell() -> BookCoverCell? {
    if let indexPath = collectionView?.indexPathForItemAtPoint(CGPointMake(collectionView!.contentOffset.x + collectionView!.bounds.width / 2, collectionView!.bounds.height / 2)) {
        if let cell = collectionView?.cellForItemAtIndexPath(indexPath) as? BookCoverCell {
            return cell
        }
    }
    return nil
}

func openBook(book: Book?) {
    let vc = storyboard?.instantiateViewControllerWithIdentifier("BookViewController") as! BookViewController
    vc.book = selectedCell()?.book
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.navigationController?.pushViewController(vc, animated: true)
        return
    })
}

我不明白问题出在哪里,非常感谢您的帮助。请温柔点,我还在学习 Swift 英语不是我的母语。

您没有正确使用 prepareForSegue。您不想在 prepareForSegue 中呈现视图控制器。使用segue.destinationViewController作为! YourViewController 改为引用它