我有一组按钮,我想使用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")
答案 0 :(得分:3)
问题是由于这一行
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)
答案 1 :(得分:0)
你已经写了两行,它们都用于视图控制器之间的转换,所以当你使用segue时,不要同时呈现viewcontroller。 您的代码中存在问题,请尝试删除以下代码。
self.present(game_level, animated: false, completion: nil)