【发布时间】:2019-09-07 23:40:40
【问题描述】:
所以我有一个带有三个 Viewcontroller 的 main.storyboard。在第一个视图控制器中,有一个带有 perform.segue 方法的开始按钮到第二个视图控制器。在第二个视图控制器中,有一个文本字段询问用户的姓名。当用户输入他的名字并点击继续按钮时,textfield.text(人名)通过prepare(for segue)方法被放入第三个视图控制器的标签中。当我第一次启动我的应用程序时,就会发生此过程。当我第二次启动我的应用程序时,在没有第一个和第二个视图控制器的“欢迎程序”的情况下显示 thirsViewcontroller,但标签消失了。
第二个ViewController的代码
@IBAction func continueButton(_ sender: Any) {
animateButton()
if enterYourNameTextfield.text != "" {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.8, execute: {
self.performSegue(withIdentifier: "thirdScreenSegue", sender: self)
}
)}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let thirdController = segue.destination as! thirdViewController
thirdController.myString = "Hello \(enterYourNameTextfield.text!)! How are you today ?"
}
第三个Viewcontroller的代码
@IBOutlet weak var helloNameLabel: UILabel!
var myString = String()
AppDelegate.swift 文件的代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let isFirst = UserDefaults.standard.bool(forKey: "isLaunched") // edited this after rmaddy's comment
var viewControllerWithIdentifier = "initialView"
if !isFirst {
UserDefaults.standard.set(true, forKey: "isLaunched")
viewControllerWithIdentifier = "firstView"
}
let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController : UIViewController = mainStoryboard.instantiateViewController(withIdentifier: viewControllerWithIdentifier) as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
sleep(2)
return true
}
【问题讨论】:
标签: swift segue viewcontroller