我使用Xcode Swift 7和2。 我想知道如何在viewDidLoad我的初始视图之前执行一个动作。 实际上我想根据参数更改初始视图(如果我已登录或未登录)... 我们应该在AppDelegate中做到吗? 谢谢
答案 0 :(得分:1)
这样你就可以从appdelegate启动特定的viewController:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("yourID")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
以这种方式分配您的故事板ID:
点击你的故事板,然后转到右侧的Identity Inspector,并提供一个故事板ID,如下图所示:
答案 1 :(得分:0)
let mainStorybord : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
Bool isAlreadyLogin = <...code for to check for already logged in or not...>
if isAlreadyLogin
{
var homeVC = storyboard.instantiateViewControllerWithIdentifier("<homeVC>") as! UIViewController
// HERE <homeVC> will be your identifier for your sugue in storyboard for initial view after login.
let nav : UINavigationController = UINavigationController(rootViewController: homeVc)
self.window?.rootViewController = nav
}
else
{
var loginVC = storyboard.instantiateViewControllerWithIdentifier("<loginVC>") as! UIViewController
// HERE <loginVC> will be your identifier for your sugue in storyboard for login view
let nav : NavigationViewController = UINavigationController(rootViewController: loginVC)
self.window?.rootViewController = nav
}
self.window?.makeKeyAndVisible()