在视图控制器中执行而不是在应用程序委托中执行:只需在您的视图控制器中获取对AppDelegate的引用,并使用正确的视图控制器作为其rootViewController重置它的window对象。
步骤1: 创建一些用户可以调整的NSUserDefaults。例如,在表格视图中放置几个按钮或开关。然后当用户点击按钮时,我们改变NSUserDefaults。
@IBAction func SwitchLaunchViewtoViewController2(sender: AnyObject) {
defaults.setObject("ViewController2", forKey: "LaunchView")
}
@IBAction func SwitchLaunchViewtoViewController1(sender: AnyObject) {
defaults.setObject("ViewController1", forKey: "LaunchView")
}
将设置视图控制器中的几个按钮连接到这些函数,我们就开始了。
步骤2:为想要作为启动视图的所有故事板设置Storyboard ID。因此,对于每个可能是初始视图控制器的视图控制器:
- 进入您的故事板。
- 单击视图控制器。
- 在右侧侧边栏中,单击报纸样式的图标,您可以在其中控制类。
- 在"Identity"部分(第三行),选中"Use Storyboard ID"(确保打开),然后在"Storyboard ID"文本字段中键入类似于"VC1"的内容。确保为每个视图控制器选择不同的Storyboard ID。
- 对于每个视图控制器重复上述步骤。
步骤3:在AppDelegate.swift文件中设置初始视图控制器。进入您的应用程序委托的func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
部分。
将以下内容添加以从您之前创建的NSUserDefault中读取:
let defaults = NSUserDefaults.standardUserDefaults()
if let launchview = defaults.stringForKey("LaunchView")
{
}
这段代码查找名为“LaunchView”的NSUserDefault字符串(在第一步中创建),如果找到匹配的NSUserDefault,则将其设置为新变量launchview。
然后,在if let launchview...
大括号内,我们要检查您将LaunchView
设置为什么。对于您在步骤1中设置为LaunchView的每个对象(例如,在本示例中我设置了"ViewController2"
和"ViewController1"
),您都必须在此处进行检查。因此,在这些大括号内,我们添加以下内容:
if launchview == "ViewController2" {
} else if launchview == "ViewController1" {
}
然后,在每个if语句内部,我们添加以下代码:
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 对我帮助如此之大。阅读下面的评论以获取更多信息。
最后注意事项:如果您正在设置视图中使用开关,请确保在该设置视图控制器的viewDidLoad
中从NSUserDefaults LaunchView
中读取用户上次选择的选项。