首页 > 解决方案 > 没有让 navigationController?.pushViewController 工作但存在吗?

问题描述

我确实有一个非故事板、100% 编码的 UIViewController、UICollectionView 和 UICollectionViewCell - 完美。

这是有问题的代码:

SceneDelegate 不确定这是否相关。

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(windowScene: windowScene)

        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        let myController = MyViewController(collectionViewLayout: layout)
        window?.rootViewController = myController
        window?.makeKeyAndVisible()
    }
.
.

ViewController 非常简单直接......

class MyViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    let data = loadOnboardingData()
.
.
override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.backgroundColor = .white
        collectionView?.register(MyPageCell.self, forCellWithReuseIdentifier: "cellId")
        collectionView?.isPagingEnabled = true
        collectionView.showsHorizontalScrollIndicator = false
        collectionView?.tag = myPageControl.currentPage

        setupMyPageControl()

    }

ViewControllerExtention 是问题所在: pushViewController 方法没有做任何事情,但模态呈现就像一个魅力,我没有得到什么是错的,为什么:

extension MyViewController: MyPageCellDelegate {
.
.
func didTabOnActionButton(title: String) {


        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        guard let homeViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController else {
            print("Coun't find controller")
            return
        }
        navigationController?.pushViewController(homeViewController, animated: true) <- NO EFFECT
        //present(homeViewController, animated: true, completion: nil) <- WORKS PERFECT!

    }

MyPageCell 我通过协议设置了委托,看起来也很好

protocol MyPageCellDelegate {
    func didTabOnActionButton(title: String)
}

class MyPageCell: UICollectionViewCell {

    var delegate: MyPageCellDelegate?

let myActionButton: UIButton = {
        let button = UIButton(type: .system)
        return button
    }()

myActionButton.addTarget(self, action: #selector(self.doAction), for: .touchUpInside)
.
.
@objc private func doAction(_ sende: Any) {
        delegate?.didTabEndOnboardingActionButton(title: "end Onboarding")
    }

所以,任何想法有什么问题:

navigationController?.pushViewController(homeViewController, animated: true)

编辑 - - - - - - - - - - - - - - - - - - - - - - - - - ------------------

正如@Michcio 在这里指出的那样:window?.rootViewController = UINavigationController(rootViewController: myController)工作一半,据我所知,我将 myController 嵌入到 UINavigationController 中,它将导航栏添加到当前和以下控制器中。

但这不是我需要的!

我需要的是一个干净简单的入职界面,即 MyViewController 和 HomeViewController 应该是一个带有选项卡和导航栏的

入职后基本上从头开始。

我曾经在以前的版本中解决这个问题,像这样编辑 AppDelegate first Method(在这个例子中我使用了 Storyboards):

extension AppDelegate {

    func showOnboarding() {
        if let window = UIApplication.shared.keyWindow, let onboardingViewController = UIStoryboard(name: "Onboarding", bundle: nil).instantiateInitialViewController() as? OnboardingViewController {
            onboardingViewController.delegate = self
            window.rootViewController = onboardingViewController
        }
    }

    func hideOnboarding() {
        if let window = UIApplication.shared.keyWindow, let mainViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() {
            mainViewController.view.frame = window.bounds
            UIView.transition(with: window, duration: 0.5, options: .transitionCrossDissolve, animations: {
                window.rootViewController = mainViewController
            }, completion: nil)
        }
    }
}

在委托本身中是这样的:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let isFirstRun = true // logic to determine goes here
    if isFirstRun {
        showOnboarding()
    }
    return true
}

但我真的没有得到新的 SceneDelegate 或者根本不理解它

如果有人可以在这里传递一些代码以供重用,我将不胜感激。

标签: swift4ios13xcode11swift5

解决方案


它没有用,因为你被设置MyViewControllerwindow.rootViewController. 只需将行更改SceneDelegate为:

window?.rootViewController = UINavigationController(rootViewController: myController)


推荐阅读