为什么我不能成功地以编程方式使用 segue?

Why I am not successfull to use segue programmatically?

我正在为 ScreenOne 使用故事板,而没有为 ScreenTwo 使用故事板,我以编程方式编码,直到现在对我来说没问题,但我仍然没有成功声明 ScreenTwo 的“storyboardIdentifier”。 Here 有一个示例,但我不明白我将如何在应用程序内部处理这个示例。任何想法将不胜感激。

截图:

屏幕二:

class ForgotPasswordEmailCheckController: UIViewController {
  var storyboardId: String {
    return (value(forKey: "ForgotPasswordEmailCheckController") as? String)!
  }

故事板控制器(也将此控制器嵌入导航控制器中):

class FirstVC: UIViewController {
    @IBOutlet weak var btn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        btn.addAction(UIAction(handler: { action in
            
            let secondVC = SecVC()
            self.navigationController?.pushViewController(secondVC, animated: true)
        }), for: .touchUpInside)
    }
}

第二个Viewcontroller:

class SecVC: UIViewController {
        
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.blue
    }
}

这是编程方式:

在 sceneDelegate 中设置您的初始导航控制器:

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

现在在 firstController 中配置你的导航栏,使用我的扩展来完成它:

extension UIViewController {

func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
    
    if #available(iOS 13.0, *) {
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithOpaqueBackground()
        navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
        navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
        navBarAppearance.backgroundColor = backgoundColor
        
        navigationController?.navigationBar.standardAppearance = navBarAppearance
        navigationController?.navigationBar.compactAppearance = navBarAppearance
        navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
        
        navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
        navigationItem.largeTitleDisplayMode = .always
        navigationController?.navigationBar.tintColor = tintColor
        navigationItem.title = title
        
    } else {
        // Fallback on earlier versions
        navigationController?.navigationBar.barTintColor = backgoundColor
        navigationController?.navigationBar.tintColor = tintColor
        navigationController?.navigationBar.isTranslucent = false
        navigationItem.title = title
    }
  }
}

这是您的第一个控制器,如下所示:

class MyController: UIViewController {

let button = UIButton() // declare your button

override func viewDidLoad() {
    super.viewDidLoad()
    
    configureNavigationBar(largeTitleColor: .white, backgoundColor: .black, tintColor: .white, title: "My Vc", preferredLargeTitle: true) // set nav bar with exetnsion
    
    view.backgroundColor = .white
        
    view.addSubview(button)
    button.backgroundColor = .systemBlue
    button.layer.cornerRadius = 9
    button.setTitle("My Button", for: .normal)
    button.addTarget(self, action: #selector (handelGoToSecondVc), for: .touchUpInside)
    button.translatesAutoresizingMaskIntoConstraints = false
    
    button.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 16).isActive = true
    button.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -16).isActive = true
    button.heightAnchor.constraint(equalToConstant: 50).isActive = true
    button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
    
}
    
@objc func handelGoToSecondVc() {
    let controller = SecondController()
    controller.title = "second Vc"
    navigationController?.pushViewController(controller, animated: true)
 }
}

第二个控制器:

class SecondController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    
    view.backgroundColor = .green
 }
}

这是结果:

完整代码:

import UIKit

class MyController: UIViewController {

let button = UIButton() // declare your button

override func viewDidLoad() {
    super.viewDidLoad()
    
    configureNavigationBar(largeTitleColor: .white, backgoundColor: .black, tintColor: .white, title: "My Vc", preferredLargeTitle: true) // set nav bar with exetnsion
    
    view.backgroundColor = .white
        
    view.addSubview(button)
    button.backgroundColor = .systemBlue
    button.layer.cornerRadius = 9
    button.setTitle("My Button", for: .normal)
    button.addTarget(self, action: #selector (handelGoToSecondVc), for: .touchUpInside)
    button.translatesAutoresizingMaskIntoConstraints = false
    
    button.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 16).isActive = true
    button.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -16).isActive = true
    button.heightAnchor.constraint(equalToConstant: 50).isActive = true
    button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
    
}
    
@objc func handelGoToSecondVc() {
    let controller = SecondController()
    controller.title = "second Vc"
    navigationController?.pushViewController(controller, animated: true)
 }
}

class SecondController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    
    view.backgroundColor = .green
 }
}

extension UIViewController {

func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
    
    if #available(iOS 13.0, *) {
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithOpaqueBackground()
        navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
        navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
        navBarAppearance.backgroundColor = backgoundColor
        
        navigationController?.navigationBar.standardAppearance = navBarAppearance
        navigationController?.navigationBar.compactAppearance = navBarAppearance
        navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
        
        navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
        navigationItem.largeTitleDisplayMode = .always
        navigationController?.navigationBar.tintColor = tintColor
        navigationItem.title = title
        
    } else {
        // Fallback on earlier versions
        navigationController?.navigationBar.barTintColor = backgoundColor
        navigationController?.navigationBar.tintColor = tintColor
        navigationController?.navigationBar.isTranslucent = false
        navigationItem.title = title
    }
  }
}