I have CollectionView in TableView. everything ok but. when I want to navigate my cell to another viewController I got error
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 error on self.navigationController?.pushViewController(bookView, animated: true) line. this is error description:
Value of 'RelatedBookTableViewCell' has no member 'navigationController'
RelatedBookTableViewCell is my custom cell class:
class RelatedBookTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
Where is my problem?
thanks.
You will either need to:
Where you pass the call from the collectionView delegate method didSelectItemAtIndexPath
to your own custom delegate/protocol on the cell that the UITableViewController which displays the cell is a delegate to. This can be set in the cellForRowAtIndexPath.
swift
Custom Protocol
protocol DisplayBookDelegate { func displayBook(bookId: String) }
In the 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)
}
}
In the cell
var delegate: DisplayBookDelegate?
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.delegate.displayBook("1")
}
bookId
in a dictionary on the notification object objc : [[NSNotificationCenter defaultCenter] postNotificationName:"kDisplayBookView" object:@{"bookId":"1"}];
swift NSNotificationCenter.defaultCenter().postNotificationName("kDisplayBookView", object: ["bookId":"1"])
Using below code you can get the Top viewcontroller and using that view controller you can perform your push operation
let objViewController = UIApplication.topViewController()!
Use this extension
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
}
}
尝试从应用程序的根控制器中推送VC。
UIApplication.sharedApplication().keyWindow?.rootViewController
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.