ios - 在显示具有清晰背景的当前 ViewController 后,无法执行到另一个 ViewController 的连接

标签 ios swift uiviewcontroller segue presentviewcontroller

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

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

我添加的内容:一个View Controller并将其设置为初始VC;一个 UIButton,它执行到 Navigation Controller 的 show segue。

Here is my storyboard

我想在 Books View Controller 出现后在后台显示 View Controller 但显然 iOS 从 View 层次结构中删除了它下面的 View Controller ,这导致了黑色背景如果我在属性检查器中设置 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 中呈现 View Controller 。使用 segue.destinationViewController 作为! YourViewController 来引用它

关于ios - 在显示具有清晰背景的当前 ViewController 后,无法执行到另一个 ViewController 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38173259/

相关文章:

ios - 在 Swift 中分配默认参数值时创建对象

iOS:self.view.bounds 没有填满整个窗口

UIViewControllerRepresentable : Navigation Title and Bar Button Items Ignored in NavigationView

ios - InApp 不适用于 iOS 5.1.1

iOS 推送通知未显示在锁屏上

iphone - 是否有可能对同一个 NSString 使用多个 NSCharacterSet 对象?

ios - 像在 iMessage 中一样在 Uitextview 中添加 UIImageview

swift - 如何添加通知?

ios - 如何取消在后台运行的线程? (iOS)

ios - 授予 ViewController 访问数据和 Controller 类的权限