简体   繁体   English

如何创建一个转接表单 UIViewController 到 UICollectionViewController? - 以编程方式

[英]How can I create a segue form UIViewController to UICollectionViewController ? - Programmatically

Can I create an app where I do some stuff and then make a segue to UICollectionViewController?我可以创建一个应用程序来做一些事情,然后对 UICollectionViewController 进行 segue 吗?

Now a get this error: " Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter' "现在得到这个错误:“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'UICollectionView 必须使用非零布局参数初始化'”

Of course, I can initiate it in SceneDelegate but I don't want to my UICollectionViewController was the first view.当然,我可以在 SceneDelegate 中启动它,但我不希望我的 UICollectionViewController 是第一个视图。

SceneDelegate when I use ViewController使用 ViewController 时的 SceneDelegate

 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
    guard let scene = (scene as? UIWindowScene) else { return }
    
    window = UIWindow(windowScene: scene)
    window?.rootViewController = ViewController()
    window?.makeKeyAndVisible() 
}

SceneDelegate when i use UICollectionViewController当我使用 UICollectionViewController 时的 SceneDelegate

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
    guard let scene = (scene as? UIWindowScene) else { return }
    
    window = UIWindow(windowScene: scene)
    window?.makeKeyAndVisible()

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    let navController = CollecionViewController(collectionViewLayout: layout)
    window?.rootViewController = navController
    
}

this is my ViewController where I want to make a segue when I press the button这是我的 ViewController,当我按下按钮时,我想在其中进行转场

import UIKit

class ViewController: UIViewController { class 视图控制器:UIViewController {

var button = UIButton()

var button2 = UIButton()

var button3 : UIButton = {
    let button = UIButton(type: .system)
    button.setTitle("Push to 3 View Controller", for: .normal)
    button.backgroundColor = .white
    button.setTitleColor(.black, for: .normal)
    button.frame = CGRect(x: 100, y: 300, width: 200, height: 50)
    button.addTarget(self, action: #selector(didTap3Button), for: .touchUpInside)
    
    return button
}()

@objc func didTap3Button() {
  let rootVC = CollecionViewController()
    let navVC = UINavigationController(rootViewController: rootVC)
    present(navVC, animated: true)
    
}

You should do exactly the thing you did in the second code snippet in didTap3Button , but instead of setting it as the rootViewController , present the navController .您应该完全按照您在 didTap3Button 的第二个代码片段中所做的操作,但不要将其设置为didTap3ButtonrootViewController present navController Your Collection VC need a layout.您的 Collection VC 需要一个布局。

@objc func didTap3Button() {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    let rootVC = CollecionViewController(collectionViewLayout: layout)
    let navVC = UINavigationController(rootViewController: rootVC)
    present(navVC, animated: true)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何实现从UICollectionViewController到UIViewController的自定义UIViewController转换? - How can I achieve this custom UIViewController Transition from UICollectionViewController to UIViewController? 我可以在一个应用程序中集成UIViewController和UICollectionViewController吗? - Can I integrate UIViewController and UICollectionViewController in one app? 如何在故事板上创建一个Segue,从编程注册的UICollectionViewCell到新的UIViewController? - How to create a Segue on the Storyboard, from a programmatically registered UICollectionViewCell to a new UIViewController? 我们如何决定使用 UICollectionViewController 还是 UIViewController? - How can we decide to use UICollectionViewController or UIViewController? 如何以编程方式从 UITableViewController 顺利切换到 UIViewController - How to segue smoothly from UITableViewController to UIViewController programmatically 如何在PageViewController的UIViewController中以编程方式使用序列 - How to use a segue, programmatically, in a UIViewController of a PageViewController 如何以编程方式从UINavigationController(storyboard)到UIViewController - How to segue from UINavigationController (storyboard) to UIViewController programmatically 如何在不使用 Storyboard segue 的情况下将项目从单独的 UIViewController 添加到 UICollectionViewController? - How to add item to a UICollectionViewController from a separate UIViewController without using Storyboard segue? 如何通过点击图像以编程方式进行搜索? - How can I programmatically segue with tapping on an Image? 如何创建从UIViewController到UISplitViewController的序列 - How to create a segue from UIViewController to a UISplitViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM