2016-08-24 44 views
0

我在TableView中有CollectionView。一切都好,但。當我想我的手機導航到另一個的viewController我有錯誤iOS Swift自定義單元格導航控制器

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中顯示我的self.navigationController .pushViewController錯誤(圖書查看,動畫:真)?行。這是錯誤的描述:

Value of 'RelatedBookTableViewCell' has no member 'navigationController' 

RelatedBookTableViewCell是我的自定義單元格類:

class RelatedBookTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout 

哪裏是我的問題嗎?

謝謝。

+0

您可以使用自定義的委託,以便UICollectionVi告訴UITableView它會告訴它的UINavigationController(通過它的UIViewController)它需要推送一個新的視圖控制器。 – Larme

+3

單元不應該充當其他單元的數據源或委託。這是一名管理員的工作。如果你想保持這個設置,把單元格選擇委託給一個'UIViewController'實例。單元格根本沒有導航控制器屬性。 – donnywals

+0

@Larme我在我的RelatedBookTableViewCell類中使用UINavigationControllerDelegate,但仍然存在錯誤。 – MohammadReza

回答

0

使用下面的代碼,你可以讓高層視圖 - 控制和使用視圖控制器,你可以執行你的推送操作

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 
    } 
} 
-1

嘗試從應用程序中的根控制器推送VC。

UIApplication.sharedApplication().keyWindow?.rootViewController 
+0

謝謝親愛的EdderNúñez,你能解釋一下嗎? – MohammadReza

+0

當你嵌套UIViews很難跟蹤導航控制器。但是你可以從代碼中的任何地方訪問應用程序本身的root,因爲它是一個Singleton。在那裏你可以推VC。 –

2

你要麼需要:

  1. 使用委託像一些模式建議。

如果您通過從的CollectionView委託方法didSelectItemAtIndexPath調用自己的自定義委託/協議對其中顯示單元的的UITableViewController爲代表的細胞。這可以在cellForRowAtIndexPath中設置。

迅速

定製協議

protocol DisplayBookDelegate { func displayBook(bookId: String) }

在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") 
} 
  1. 使用的通知,並在字典中傳遞bookId通知對象

objc上:[[NSNotificationCenter defaultCenter] postNotificationName:"kDisplayBookView" object:@{"bookId":"1"}];

迅速NSNotificationCenter.defaultCenter().postNotificationName("kDisplayBookView", object: ["bookId":"1"])

相關問題