为什么第二个视图控制器在加载入口视图后没有出现,但是通过单击按钮,他出现了?
Why does the second view controller didn´t appear after loading the entry view, but by clicking on a button, he does?
为什么第二个视图控制器在加载入口视图后没有出现,但是通过点击按钮出现了?
这是我的代码:
@IBAction func Pressed(_ sender: UIButton) {
var firstName = FirstName.text;
var lastName = LastName.text;
let defaults = UserDefaults.standard;
defaults.set(firstName, forKey: "FN");
defaults.set(lastName, forKey: "LN");
loadNextView()
}
override func viewDidLoad() {
super.viewDidLoad()
self.FirstName.text = "TESTAUSGABE";
let defaults = UserDefaults.standard;
var fn = defaults.object(forKey: "FN")
var ln = defaults.object(forKey: "LN")
if(fn==nil){
self.FirstName.placeholder="Nicht angegeben"
}
else {
self.FirstName.text = fn as? String
loadNextView()
}
if(ln==nil){
self.LastName.placeholder="Nicht angegeben"
}
else{
self.LastName.text = ln as? String
self.Pressed(self.SaveButton);
}
}
func loadNextView(){
let storyboard: UIStoryboard = UIStoryboard(name:"Main", bundle:nil);
let newViewControler = storyboard.instantiateViewController(withIdentifier: "View2") as UIViewController
self.present(newViewControler, animated: true, completion: nil)
}
我想检查首选项,如果它们已经设置,那么它应该会自动出现在另一个视图中。
还是有其他我不知道的方法?
Prsenting VCs 在 viewDidLoad
中不起作用,因为视图层次结构尚未完成,您可以尝试实现 viewDidAppear
或 viewWillAppear
var runOnce = true
//
override func viewDidAppear(_ animated:Bool){
super.viewDidAppear(animated)
if runOnce {
loadNextView() // here read default value
runOnce = false
}
}
为什么第二个视图控制器在加载入口视图后没有出现,但是通过点击按钮出现了?
这是我的代码:
@IBAction func Pressed(_ sender: UIButton) {
var firstName = FirstName.text;
var lastName = LastName.text;
let defaults = UserDefaults.standard;
defaults.set(firstName, forKey: "FN");
defaults.set(lastName, forKey: "LN");
loadNextView()
}
override func viewDidLoad() {
super.viewDidLoad()
self.FirstName.text = "TESTAUSGABE";
let defaults = UserDefaults.standard;
var fn = defaults.object(forKey: "FN")
var ln = defaults.object(forKey: "LN")
if(fn==nil){
self.FirstName.placeholder="Nicht angegeben"
}
else {
self.FirstName.text = fn as? String
loadNextView()
}
if(ln==nil){
self.LastName.placeholder="Nicht angegeben"
}
else{
self.LastName.text = ln as? String
self.Pressed(self.SaveButton);
}
}
func loadNextView(){
let storyboard: UIStoryboard = UIStoryboard(name:"Main", bundle:nil);
let newViewControler = storyboard.instantiateViewController(withIdentifier: "View2") as UIViewController
self.present(newViewControler, animated: true, completion: nil)
}
我想检查首选项,如果它们已经设置,那么它应该会自动出现在另一个视图中。
还是有其他我不知道的方法?
Prsenting VCs 在 viewDidLoad
中不起作用,因为视图层次结构尚未完成,您可以尝试实现 viewDidAppear
或 viewWillAppear
var runOnce = true
//
override func viewDidAppear(_ animated:Bool){
super.viewDidAppear(animated)
if runOnce {
loadNextView() // here read default value
runOnce = false
}
}