swift4 - 没有让 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 或者根本不理解它
如果有人可以在这里传递一些代码以供重用,我将不胜感激。
解决方案
它没有用,因为你被设置MyViewController
为window.rootViewController
. 只需将行更改SceneDelegate
为:
window?.rootViewController = UINavigationController(rootViewController: myController)
推荐阅读
- youtube-api - 使用 youtube API 从一些 Youtube 频道获取最新视频
- visual-studio - 如何在visual studio 2019上使用winapi创建打开文件资源管理器的按钮
- python - 如何将 pandas 数据帧发送到 flask_restful api?
- twitter - 为什么 tweepy api.search 使用 result_type="popular" 时每页最多只返回 15 条推文?
- sql - 使用窗口函数和过滤器将 1 个特定值返回到所有行
- linux - golang 默认选择哪个加载器
- python - 如何从字典中减去字符串十六进制值?
- python - Python SQL:错误十进制('1367')不是 JSON 可序列化的
- jquery - 将 html 附加到找到“包含”的文本
- reactjs - 如何在 React 中上传视频并验证大小并将文件作为发布请求发送?