iOS Swift自定义Cell NavigationController

时间:2016-08-24 08:01:59

标签: ios objective-c swift uitableview uicollectionviewcell

我在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在self.navigationController上显示错误?.pushViewController(bookView,animated:true)行。这是错误描述:

Value of 'RelatedBookTableViewCell' has no member 'navigationController'

RelatedBookTableViewCell是我的自定义单元类:

class RelatedBookTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout

我的问题在哪里?

感谢。

3 个答案:

答案 0 :(得分:2)

您需要:

  1. 使用像某些人建议的委托模式。
  2. 将来自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
    2. objc [[NSNotificationCenter defaultCenter] postNotificationName:"kDisplayBookView" object:@{"bookId":"1"}];

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

答案 1 :(得分:0)

使用以下代码,您可以获得Top viewcontroller并使用该视图控制器执行推送操作

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

答案 2 :(得分:-1)

尝试从应用程序中的根控制器推送VC。

UIApplication.sharedApplication().keyWindow?.rootViewController