gpt4 book ai didi

swift4 - 没有让 navigationController?.pushViewController 工作但存在吗?

转载 作者:行者123 更新时间:2023-12-03 23:50:44 25 4
gpt4 key购买 nike

我确实有一个 Non-Storyboard 、100% 编码的 UIViewController、UICollectionView 和 UICollectionViewCell - 完美。

这是有问题的代码:

场景委托(delegate)
不知道这是否相关,寿。

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

View Controller
非常简单直接...
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()

}

View Controller 扩展
这就是问题所在: 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!

}

我的页面单元
我通过协议(protocol)设置了代表,看起来也很好
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 中,它将导航栏添加到当前和以下 Controller 中。

但这不是我需要的!

我需要的是一个干净和简单的入门指南,即 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)
}
}
}

在委托(delegate)本身中是这样的:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let isFirstRun = true // logic to determine goes here
if isFirstRun {
showOnboarding()
}
return true
}

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

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

最佳答案

它没有用,因为你设置了 MyViewControllerwindow.rootViewController .只需更改 SceneDelegate 中的行到:
window?.rootViewController = UINavigationController(rootViewController: myController)

关于swift4 - 没有让 navigationController?.pushViewController 工作但存在吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58292910/

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