owl*_*ipe 7 storyboard ios swift
我在堆栈溢出时看到了很长的Objective-C答案,但没有快速的答案.
如何从视图控制器以swift方式以编程方式更改初始视图控制器?
我认为它会是这样的:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
storyboard.setInitialViewController(identifier: ViewController())
Run Code Online (Sandbox Code Playgroud)
但不,这没有任何作用.第一行很好,但第二行的功能不存在.
Ank*_*oel 12
要在视图控制器中而不是在应用程序委托中执行:只需在视图控制器中获取对AppDelegate的引用,并使用右视图控制器重置它的窗口对象,因为它是rootviewController.
第1步:制作一些用户可以调整的NSUserDefaults.几个按钮,表视图中的一些开关,一些东西.然后,当用户点击按钮时,我们更改NSUserDefault.
@IBAction func SwitchLaunchViewtoViewController2(sender: AnyObject) {
defaults.setObject("ViewController2", forKey: "LaunchView")
}
@IBAction func SwitchLaunchViewtoViewController1(sender: AnyObject) {
defaults.setObject("ViewController1", forKey: "LaunchView")
}
Run Code Online (Sandbox Code Playgroud)
将设置视图控制器中的几个按钮挂钩到这些功能,我们已经开始了.
第2步:为您希望能够设置为启动视图的所有故事板设置故事板ID.因此,对于每个可能是初始视图控制器的View Controller:
- 进入你的故事板.
- 单击视图控制器.
- 在右侧边栏中,单击您可以控制课程的类似报纸的图标.
- 在"身份"部分(第三行)中,选中"使用故事板ID"(确保它已打开),然后在"故事板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")
{
}
Run Code Online (Sandbox Code Playgroud)
这将查找名为"LaunchView"的NSUserDefault字符串(您在步骤1中创建),并在找到匹配的NSUserDefault时将其设置为新变量launchview.
然后,在if let launchview...括号内,我们想要检查您设置的内容LaunchView.对于您在步骤1中设置为LaunchView的每个对象(在示例中,我做了"ViewController2"和"ViewController1"),您必须在此处检查它.所以,在这些括号内,我们添加:
if launchview == "ViewController2" {
} else if launchview == "ViewController1" {
}
Run Code Online (Sandbox Code Playgroud)
然后,在每个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()
Run Code Online (Sandbox Code Playgroud)
当应用程序在后台运行一段时间后完成加载时,这将打开所选窗口.
你didFinishLoadingWithOptions的AppDelegate的完成部分可能如下所示:(不要只是复制和粘贴,阅读上面的说明)
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
}
Run Code Online (Sandbox Code Playgroud)
我希望这对你有所帮助,非常感谢Ankit Goel,他帮我解决了这个问题.阅读下面的评论了解更多信息.
最后一点说明:如果您在设置视图中使用开关,请确保在您从NSUserDefault读取的设置视图控制器的viewDidLoad中,LaunchView用户最后选择了哪一个.
针对Swift 3进行了更新
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: ViewController())
return true
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16805 次 |
| 最近记录: |