我在TableView中有CollectionView。一切都好,但是。当我想将我的单元格导航到另一个viewController时,我遇到了错误
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let bookView: BookSingleController = storyboard.instantiateViewControllerWithIdentifier("BookSingle") as! BookSingleController
self.navigationController?.pushViewController(bookView, animated: true)
bookView.bookID = "\(self.books[indexPath.row].id)"
print(self.books[indexPath.row].id)
}
Xcode show me self.NavigationController?.PushViewController(bookView,Animated:true)行出现错误。这是错误说明:
Value of 'RelatedBookTableViewCell' has no member 'navigationController'
RelatedBookTableViewCell是我的自定义单元格类:
class RelatedBookTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
我的问题在哪里?
谢了。
使用下面的代码,您可以获得顶部的视图控制器,并使用该视图控制器,您可以执行推送操作
let objViewController = UIApplication.topViewController()!
使用此扩展名
extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(presented)
}
return base
}
}
null
其中,您将来自collectionView委托方法
斯威夫特
自定义协议
在tableViewController中
class tableViewController: UITableViewController, DisplayBookDelegate {
...
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.delegate = self
return UITableViewCell()
}
func displayBook(let bookId) {
self.navigationController?.pushViewController(bookView, animated: true)
}
}
在单元格中
var delegate: DisplayBookDelegate?
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.delegate.displayBook("1")
}
objc:
swift
尝试从应用程序中的根控制器推入VC。
UIApplication.sharedApplication().keyWindow?.rootViewController