viewController 在 segue 通过之前出现

viewController is presented before the segue has been passed

我有一组按钮,我想使用 segue 将它们的标题发送到下一页。这是我所做的:

// in my first page i have : 
 @IBAction func btn_language_action(_ sender: UIButton) {
        let chosen_language = sender.currentTitle!
        print(chosen_language)
        let game_level = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "game_level") as! game_level_viewController
        self.present(game_level, animated: false, completion: nil)
        performSegue(withIdentifier: "GL", sender: chosen_language)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let game_level_viewController = segue.destination as! game_level_viewController
        game_level_viewController.language = sender as? String

    }

在第二个视图中我有:

  @IBOutlet weak var lang: UILabel!
    var language : String?
    override func viewDidLoad() {
        super.viewDidLoad()
       setUI()
    }

    func setUI (){

        lang.text = language
        print(language )

    }

不过我可以成功传递信息,但是在我收到发送的内容之前视图已经加载。知道我该如何解决这个问题吗? 在控制台中我有:

language ==>  nil
language ==>  Optional("Persian")

问题出在这一行

self.present(game_level, animated: false, completion: nil)

为什么即使您已将 segue 提供给控制器,您仍要显示此控制器

删除这两行,一切都会好起来的

 let game_level = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "game_level") as! game_level_viewController
        self.present(game_level, animated: false, completion: nil)

你已经写了两行,它们都用于视图控制器之间的转换,所以当你使用 segue 时,不要同时出现 viewcontroller。 您的代码中的问题出在这一行,请尝试删除以下代码。

self.present(game_level, animated: false, completion: nil)