【问题标题】:Pushing to new ViewController from collectionView Cell didSelectItemAt programmatically - swift以编程方式从 collectionView Cell didSelectItemAt 推送到新的 ViewController - swift
【发布时间】:2018-07-20 04:18:35
【问题描述】:

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

这是我在appDelegate中设置rootViewController

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

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

【问题讨论】:

  • 我已经将UICollectionViewController 设置为 UINavigationController
  • window = UIWindow(frame: UIScreen.main.bounds) let flow = UICollectionViewFlowLayout() let collectionView = ViewController(collectionViewLayout: flow) let navigation = UINavigationController(rootViewController: collectionView) window?.rootViewController = navigation window?.makeKeyAndVisible()

标签: ios swift uinavigationcontroller uicollectionviewcell uicollectionviewflowlayout


【解决方案1】:

首先你必须确保你当前的视图控制器有导航视图控制器,你可以通过在 viewDidLoad 方法 print(self.navigationViewController) 中打印对象来检查。如果打印 nil ,那么您必须制作导航控制器,然后执行以下步骤 - 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方法中

【讨论】:

  • 他没有使用情节提要。他在 appdelegate 中设置了 rootViewController
  • 将此行粘贴到您的 AppDelegate 的 applicationDidFinish 方法中。 self.window = UIWindow(frame: UIScreen.main.bounds) self.window?.makeKeyAndVisible() 窗口?.rootViewController = UINavigationController(rootViewController: YourCollectionViewCotroller)
  • @Ashish 已添加。我也编辑了我的问题。当前视图一直没有推送到另一个视图,但是我确实调用了语句的断点(函数 pushView() 的语句)但它没有推送....
  • @VisalSambo 在您的代码中似乎您尚未初始化刚刚声明的 viewcontroller 对象。调用 pushView() 方法,然后调用 self 或将其粘贴到 didSelect "pushView()"
  • @Ashish 你的意思是在 didSelect 我应该输入‘self.viewController.pushView()’?
【解决方案2】:

请试试这个:

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

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

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

【讨论】:

  • 他没有使用故事板。
【解决方案3】:

改变

viewController?.pushView()

self.pushView()

【讨论】:

  • 摆脱毫无意义的viewController属性。你是视图控制器,self
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-20
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 2020-02-03
相关资源
最近更新 更多