【发布时间】:2016-07-03 18:49:29
【问题描述】:
我跟随 Ray Wenderlich tutorial 了解如何创建 iOS 书籍动画,现在我正在尝试对其进行一些更改。
原始项目由Navigation Controller、Books View Controller - 显示一系列书籍,通过单击一本书可以打开它 - 和Book View Controller - 显示所选书籍打开。
我添加的内容: View Controller 并将其设置为初始 VC;一个UIButton,它对Navigation 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 的 segue。所以显然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,英语不是我的母语。
【问题讨论】:
标签: ios swift uiviewcontroller segue presentviewcontroller