首页 文章

iOS Swift自定义Cell NavigationController

提问于
浏览
0

我在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 回答

  • -1

    使用下面的代码,您可以获得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

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

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

    您将需要:

    • 使用像某些人建议的委托模式 .

    您将来自collectionView委托方法 didSelectItemAtIndexPath 的调用传递给您自己的自定义委托/协议,其中单元格显示单元格的UITableViewController是委托 . 这可以在cellForRowAtIndexPath中设置 .

    迅速

    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

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

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

相关问题