navigationController?.pushViewController 不工作 (navigationController?.pushViewController is not working)


問題描述

navigationController?.pushViewController 不工作 (navigationController?.pushViewController is not working)

我有一個集合視圖控制器。在 collectionView 單元格 中,我有一個標籤,我可以點擊它來push 到 nextViewController。

我知道 navigationController 中的問題。但我是新來的,所以無法修復。希望你們能幫助我。

這是我的 SceneDelegate:

    let layout = UICollectionViewFlowLayout()
    // Create the root view controller as needed
    let nc = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))

    let win = UIWindow(windowScene: winScene)
    win.rootViewController = nc
    win.makeKeyAndVisible()
    window = win

和我的標籤:

    let text = UILabel()
    text.text = "something"
    text.isUserInteractionEnabled = true
    self.addSubview(text)

    let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PopularCellTwo.labelTapped))
    text.addGestureRecognizer(gestureRecognizer)
}

    @objc func labelTapped() {
        let nextVC = NextViewController()
        self.navigationController?.pushViewController(nextVC, animated: true)
        print("labelTapped tapped")

    }

我還添加了屏幕截圖。當我點擊“某事”時 它應該進入下一頁。[1]:https://i.stack.imgur.com/4oYwb.png


參考解法

方法 1:

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

what self are you referring to ? because you cant make push in child class you have HomeController i assume its your parent controller .

just try to debug what self is this could attempt by debugging or debug by condition

print (self)
if (self.isKind(of: YourParentController.self)) {
// make push
}

or try to check , see if navigationcontroller somehow has nil value

方法 2:

Here is how you do it using closures. I've created a closure parameter in UICollectionViewCell sub‑class. When the label gesture target is hit I call the closure which then executed the navigation in HomeController.

class HomeController: UICollectionViewController {
    //...
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) ‑> UICollectionViewCell {
        let cell = // dequeue cell
        cell.labelTap = { [weak self] in
            guard let self = self else { return }
            let nextVC = NextViewController()
            self.navigationController?.pushViewController(nextVC, animated: true)
            print("navigated")
        }
        return cell
    }
}

class CollectionViewCell: UICollectionViewCell {

    var labelTap: (() ‑> Void)?
    @objc func labelTapped() {
        print("labelTapped tapped")
        labelTap?()
    }
}

方法 3:

You can use delegate or closure to do this

class ItemCollectionViewCell: UICollectionViewCell {
   var onTapGesture: (() ‑> ())?
}

Then in your function you do

    @objc func labelTapped() {

         onTapGesture?()
    }

And in your controller

class HomeController: UICollectionViewController {
    //...
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) ‑> UICollectionViewCell {
        let cell = // dequeue cell
        cell.onTapGesture = { [unowned self] in
            let nextVC = NextViewController()
            self.navigationController?.pushViewController(nextVC, animated: true)

        }
        return cell
    }
}

(by AldiyarAdli RFrankensteinJawad Ali)

參考文件

  1. navigationController?.pushViewController is not working (CC BY‑SA 2.5/3.0/4.0)

#uicollectionviewcell #XCode #swift #uicollectionview #uinavigationcontroller






相關問題

Sel UICollectionView tidak terlihat (UICollectionView cells are invisible)

UICollectionViewCell裡面的UIWebView不調用Delegate方法 (UIWebView inside UICollectionViewCell does not call Delegate Method)

Bộ sưu tập xem vấn đề thay đổi kích thước iPhone 4 và iPhone 5 (Collection view resizing issue iPhone 4 vs iPhone 5)

集合視圖單元格未出現 (Collection View Cells not appearing)

UICollectionView 單元格大小 ContentView 問題 (UICollectionView cell size ContentView Issue)

在 Swift 中使用 CollectionView 進行網格佈局 (Grid layout with CollectionView in Swift)

如何為collectionviewcell的刪除自定義動畫? (How to custom animation for collectionviewcell's deletion?)

檢測位於另一個 uicollectionview 內的 uicollectionView 的哪個 UICollectionViewCell 在中心 (Detect which UICollectionViewCell of a uicollectionView that is inside another uicollectionview is in the center)

navigationController?.pushViewController 不工作 (navigationController?.pushViewController is not working)

XCode UICollectionViewCell 在模擬器上看起來比在故事板中小得多 (XCode UICollectionViewCell appearing much smaller on simulator than in storyboard)

CollectionViewCell 未出現在 collectionView 中 (CollectionViewCell not appearing in collectionView)

由 LPLinkView 和 UIImageView 組成的單元格的 CollectionView 速度很慢,並且在滾動時會重新加載數據 (CollectionView with Cells made up of LPLinkView's and UIImageView's is Slow and Reloads Data While Scrolling)







留言討論