加载特定视图

时间:2017-01-04 23:49:09

标签: ios swift

我对整个iOS开发有点新鲜。我试图创建一个期刊应用程序,但是对于许多人可能比我更了解的事情而苦苦挣扎。我试图根据参数加载特定的视图。在我的情况下,在应用程序首次加载时,我想加载"设置"查看默认页面。但是在第一次加载之后(也就是在用户设置了他们的个人资料之后),我想加载" Master" view(显示必要信息的页面,例如创建新的日记帐分录等)

目前我在viewDidLoad中有这段代码:

 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let userDefaults = UserDefaults.standard
        if userDefaults.value(forKey: "firstTimeLoad") as? Bool != nil {
// FIXME: Add code which loads the setup view

            userDefaults.setValue(true, forKey: "firstTimeLoad")
            userDefaults.synchronize()
        } else if userDefaults.value(forKey: "firstTimeLoad") as? Bool == nil {
// FIXME: Add code which hides the Setup view and loads the Master view.
        }
    }

我尝试过一些事情,例如使用loadView加载特定视图,但似乎没有任何效果。

2 个答案:

答案 0 :(得分:0)

啊,你应该通过AppDelegate.swift

来做到这一点
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    let userDefaults = UserDefaults.standard
    //If you have key, push your master view.
    if userDefaults.value(forKey: "firstTimeLoad") as? Bool != nil {
        return loadMasterViewController()
    } else {
        userDefaults.setValue(true, forKey: "firstTimeLoad")
        userDefaults.synchronize()
    }
    return true
}

func loadMasterViewController() -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let masterViewController = storyboard.instantiateViewController(withIdentifier: "MasterViewController")
    self.window?.rootViewController = masterViewController
    self.window?.makeKeyAndVisible()
    return true
}

答案 1 :(得分:0)

很容易做到:

结果:

enter image description here

在你的VC1中:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

        let userDefaults = UserDefaults.standard
        userDefaults.setValue(false, forKey: "firstTimeLoad")  // you can set firstTimeLoad's value for vc2 to load different view.
        userDefaults.synchronize()
    }

}

在你的VC2中:

import UIKit

class ViewController2: UIViewController {

var view1:UIView?
var view2:UIView?

override func viewDidLoad() {

    super.viewDidLoad()

    view1 = UIView.init(frame: UIScreen.main.bounds)
    view1?.backgroundColor = .red
    view2 = UIView.init(frame: UIScreen.main.bounds)
    view2?.backgroundColor = .blue


    // Do any additional setup after loading the view, typically from a nib.
    let userDefaults = UserDefaults.standard
    if userDefaults.value(forKey: "firstTimeLoad") as? Bool == false {
        // FIXME: Add code which loads the setup view

        self.view.addSubview(view1!)


        userDefaults.setValue(true, forKey: "firstTimeLoad")  // attention: there I changed the firstTimeLoad value to true, so next time you run in vc1, will load the blue view.
        userDefaults.synchronize()
    } else if userDefaults.value(forKey: "firstTimeLoad") as? Bool == true {
        // FIXME: Add code which hides the Setup view and loads the Master view.

        self.view.addSubview(view2!)
    }
}
}
相关问题