2016-08-24 21 views
0

私はTableViewにCollectionViewを持っています。すべてが大丈夫です。 ?行:私は別のViewControllerに自分のセルを移動したいとき、私はiOS SwiftカスタムセルNavigationController

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は私に(真アニメーションbookView、)self.navigationController .pushViewController上のエラーを示し、エラーました。これはエラーの説明です:

Value of 'RelatedBookTableViewCell' has no member 'navigationController' 

RelatedBookTableViewCellは私のカスタムセルクラスです:

class RelatedBookTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout 

私の問題はありますか?

ありがとうございました。

+0

、UICollectionVi ewはUITableViewに、新しいView Controllerをプッシュする必要があることをUINavigationController(UIViewControllerのトラフ)に通知します。 – Larme

+3

セルは、他のセルのデータソースまたはデリゲートとして機能しません。これはコントローラの仕事です。この設定を保持したい場合は、セル選択を 'UIViewController'インスタンスに委譲します。セルには、ナビゲーションコントローラのプロパティはありません。 – donnywals

+0

@Larme私はRelatedBookTableViewCellクラスでUINavigationControllerDelegateを使用しますが、エラーは依然として存在します。 – MohammadReza

答えて

0

あなたはトップのViewControllerを取得し、そのビューコントローラを使用することができ、コードの下に使用して、あなたはあなたがする必要がありますいずれか

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

UIViewをネストすると、ナビゲーションコントローラの追跡が難しくなります。しかし、シングルトンなので、コード内のどこからでもアプリケーション自体からrootにアクセスできます。そこにVCを押し込むことができます。 –

2

この拡張機能を使用し

let objViewController = UIApplication.topViewController()! 

あなたのプッシュ操作を実行できます。

  1. デリゲートを使用するいくつかのようなパターンが示唆している。あなたはセルを表示するのUITableViewControllerがに委譲され、細胞に、独自のカスタムデリゲート/プロトコルにcollectionViewデリゲートメソッドdidSelectItemAtIndexPathからの呼び出しを渡す

。これは、cellForRowAtIndexPathで設定できます。

迅速

カスタムプロトコル細胞において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) 
    } 
} 

protocol DisplayBookDelegate { func displayBook(bookId: String) }

var delegate: DisplayBookDelegate? 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    self.delegate.displayBook("1") 
} 
  1. 使用の通知と通知オブジェクト

にObjCに辞書にbookIdを渡す:カスタムデリゲートを使用することができます[[NSNotificationCenter defaultCenter] postNotificationName:"kDisplayBookView" object:@{"bookId":"1"}];

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

関連する問題