gpt4 book ai didi

ios - 如何从 UICollectionViewCell 中的 UITableViewCell 内部推送 View ?

转载 作者:行者123 更新时间:2023-11-30 11:19:08 24 4
gpt4 key购买 nike

我有一个UITableView UICollectionViewCell 里面我正在尝试从 UITableViewCell 推出新的 View tableView(_:didSelectRowAt:)里面方法。但是,我无法访问 navigationController 。我将如何导航到新 View ?

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
}

最佳答案

获取当前 View 的父 Controller -

//MARK: get parent controller...
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if let viewController = parentResponder as? UIViewController {
return viewController
}
}
return nil
}
}

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

let viewController = GymLearnMoreViewController(nibName: "GymLearnMoreViewController", bundle: nil) //Your View controller instance
parentViewController?.navigationController?.pushViewController(viewController, animated: true)

来源苹果:-

https://developer.apple.com/documentation/uikit/uiresponder/1621099-next https://developer.apple.com/documentation/uikit/uiresponder?changes=_9

关于ios - 如何从 UICollectionViewCell 中的 UITableViewCell 内部推送 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51471584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com