Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从UICollectionViewCell中的UITableViewCell内部推送视图?_Ios_Swift_Uitableview_Uinavigationcontroller - Fatal编程技术网

Ios 如何从UICollectionViewCell中的UITableViewCell内部推送视图?

Ios 如何从UICollectionViewCell中的UITableViewCell内部推送视图?,ios,swift,uitableview,uinavigationcontroller,Ios,Swift,Uitableview,Uinavigationcontroller,我在UICollectionViewCell中有一个UITableView,我正试图从tableView(\uDidSelectRowat:)方法中的UITableViewCell中推送一个新视图。但是,我无法访问navigationController。我将如何导航到新视图 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { var item = Item(name: "")

我在
UICollectionViewCell
中有一个
UITableView
,我正试图从
tableView(\uDidSelectRowat:)
方法中的
UITableViewCell
中推送一个新视图。但是,我无法访问
navigationController
。我将如何导航到新视图

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    var item = Item(name: "")

    switch indexPath.section {
    case 0:
        item = array1![indexPath.item]
    case 1:
        item = array2![indexPath.item]
    default:
        break
    }

    let layout = UICollectionViewFlowLayout()
    let newView = NewCollectionView(collectionViewLayout: layout)
    newView.itemOfInterest = item

    // Can't reference navigationController
}
为此

一,。您需要在添加tableview的collectionviewcell中声明协议

二,。在添加collectionview的类上实现协议方法。并在单击“从此处开始”时处理导航

三,。在调用collectionviewCellForrow at indexpath方法时设置collectionview单元格委托,并在collectionview单元格内单击tableview项时调用委托方法

用法:- 获取navigationController引用为
parentViewController?.navigationController

let viewController = GymLearnMoreViewController(nibName: "GymLearnMoreViewController", bundle: nil) //Your View controller instance
parentViewController?.navigationController?.pushViewController(viewController, animated: true)
资料来源:苹果公司:-


一种方法是使用
通知中心
,您可以在
ParentViewController
中编写:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(methodNavigate(notification:)), name: NSNotification.Name(rawValue: "NavigateToSecondView"), object: nil)
}

@objc func methodNavigate(notification: NSNotification) {
    Write your push segue code here.
}
不要忘记在
视图中删除观察者:

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)

    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "NavigateToSecondView"), object: nil)
}
你也可以通过它传递数据

然后在
中选择rowat

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NavigateToSecondView"), object: "Send Data Here")
但我会建议使用协议是更好、更优雅的方式。 您可以在internet上获得大量教程和示例:


希望能有帮助。愉快的编码。

您可以在didSelect中创建委托方法,该方法将在您的viewcontroller中实现,您可以在其中推送。您还可以在viewcontroller中实现tableview委托的删除方法。您还可以实现自定义委托的选项,这些选项在ViewController中调用。
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NavigateToSecondView"), object: "Send Data Here")