如何从视图控制器以swift方式以编程方式更改初始视图控制器?
我认为它会像这样:
let storyboard = UIStoryboard(name: "Main",bundle: nil) storyboard.setInitialViewController(identifier: ViewController())
但不,这没有做任何事情.第一行很好,但第二行的功能不存在.
解决方法
第1步:制作一些用户可以调整的NSUserDefaults.几个按钮,表视图中的一些开关,一些东西.然后,当用户点击按钮时,我们更改NSUserDefault.
@IBAction func SwitchLaunchViewtoViewController2(sender: AnyObject) { defaults.setObject("ViewController2",forKey: "LaunchView") } @IBAction func SwitchLaunchViewtoViewController1(sender: AnyObject) { defaults.setObject("ViewController1",forKey: "LaunchView") }
在设置视图控制器中挂起了几个按钮,直到这些功能,我们已经开始了.
第2步:为您希望能够设置为启动视图的所有故事板设置故事板ID.因此,对于每个可能是初始视图控制器的View Controller:
– 进入你的故事板.
– 单击视图控制器.
– 在右侧边栏中,单击您可以控制班级的类似报纸的图标.
– 在“身份”部分(第三行)中,选中“使用故事板ID”(确保它已打开),然后在“故事板ID”文本字段中键入类似“VC1”的内容.确保为每个视图控制器选择不同的Storyboard ID.
– 为每个视图控制器重复.
第3步:在AppDelegate.swift文件中设置初始视图控制器.进入func应用程序(应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) – >您的应用委托的Bool部分.
let defaults = NSUserDefaults.standardUserDefaults() if let launchview = defaults.stringForKey("LaunchView") { }
这将查找名为“LaunchView”的NSUserDefault字符串(您在步骤1中创建),如果找到匹配的NSUserDefault,则将其设置为新变量launchview.
然后,在if let launchview …括号内,我们想要检查你将LaunchView设置为什么.对于您在步骤1中设置为LaunchView的每个对象(在示例中,我执行了“ViewController2”和“ViewController1”),您必须在此处检查它.所以,在这些括号内,我们添加:
if launchview == "ViewController2" { } else if launchview == "ViewController1" { }
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds) let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil) // this assumes your storyboard is titled "Main.storyboard" let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("YOUR_VC_IDENTIFIER") as! YourViewController // inside "YOUR_VC_IDENTIFIER" substitute the Storyboard ID you created in step 2 for the view controller you want to open here. And substitute YourViewController with the name of your view controller,like,for example,ViewController2. appDelegate.window?.rootViewController = yourVC appDelegate.window?.makeKeyAndVisible()
当应用程序在后台运行一段时间后完成加载时,这将打开所选窗口.
AppDelegate的完成的didFinishLoadingWithOptions部分可能如下所示:
(不要只是复制粘贴,阅读上面的说明)
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let defaults = NSUserDefaults.standardUserDefaults() if let launchview = defaults.stringForKey("LaunchView") { if launchview == "ViewController1" { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds) let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil) let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("VC1") as! ViewController1 appDelegate.window?.rootViewController = yourVC appDelegate.window?.makeKeyAndVisible() } else if launchview == "ViewController2" { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds) let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil) let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("VC1") as! ViewController1 appDelegate.window?.rootViewController = yourVC appDelegate.window?.makeKeyAndVisible() } } return true }
我希望这对你有所帮助,非常感谢Ankit Goel,他非常帮助我.请阅读下面的评论以获取更多信息.
最后一点说明:如果您在设置视图中使用开关,请确保在您从NSUserDefault LaunchView读取的设置视图控制器的viewDidLoad中,用户最后选择了哪一个.