繁体   English   中英

以编程方式从collectionView Cell didSelectItemAt推送到新的ViewController-Swift

[英]Pushing to new ViewController from collectionView Cell didSelectItemAt programmatically - swift

我试图创建一个简单的应用程序, UICollectionViewController编程方式实现UICollectionViewController 我创建了一些单元格,当用户单击特定单元格时,应将其移至另一个控制器。 但是我面临着哪个视图没有推送到下一个视图控制器的问题。

这里是我的设置RootViewController的appDelegate

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()
    let flow = UICollectionViewFlowLayout()
    let collectionView = ViewController(collectionViewLayout: flow)
    let navigation = UINavigationController(rootViewController: collectionView)
    window?.rootViewController = navigation

这是我创建UICollectionView

private let cellId = "cellid"
var viewController: ViewController?
override func viewDidLoad() {
    super.viewDidLoad()
    collectionView?.backgroundColor = .white
    collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
    // Do any additional setup after loading the view, typically from a nib.
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
    cell.backgroundColor = .red
    return cell
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: 210)
}

这是我如何控制用户何时点击特定内容

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print(indexPath.item)
    viewController?.pushView()
}

func pushView() {
    let controller = UINavigationController(rootViewController: DetailController())
    self.navigationController?.pushViewController(controller, animated: true)
}

即使函数被调用,但它永远不会推送到新的视图控制器

请尝试以下方法:

let objMainController : yourViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourViewController") as! yourViewController

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = objMainController

请从情节提要中嵌入导航控制器。 可能会对您有所帮助。谢谢。

首先,您必须确保当前的视图控制器具有导航视图控制器,您可以通过在viewDidLoad方法print(self.navigationViewController)中打印对象来进行检查。 如果打印为零,则必须使导航控制器执行以下步骤-SelectYourVicotroller->转到编辑器菜单选项->嵌入到->导航控制器

func pushView() {

    let controller =  DetailController()
    //OR

//Add the storyboard identifier of your view controller - "DetailController"

    let controller =  self.storyboard.instantiateViewController(withIdentifier: "DetailController") as! DetailController

    self.navigationController?.pushViewController(controller, animated:true)

}

将此方法调用为didSelect方法

更改

viewController?.pushView()

self.pushView()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM