从View Controller - Swift调用TabBarController委托

时间:2015-03-29 22:00:49

标签: ios swift

所以我有一个基于Parse后端的iOS应用程序。我在UITabBarController中设置了AppDelegate,这可以正常使用。

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var tabBarController: UITabBarController?
var array: [AnyObject] = []

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    Parse.setApplicationId("1040205491TFSDJT43T49T349F3I4T3J")
    var navigationBarAppearance = UINavigationBar.appearance()
    navigationBarAppearance.tintColor = uicolorFromHex(0xffffff)
    navigationBarAppearance.barTintColor = uicolorFromHex(0x4183D7)
    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
    let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    navigationBarAppearance.titleTextAttributes = titleDict
    //navigationBarAppearance.clipsToBounds = true;
    let tabBarController = UITabBarController()
    let storyboard = UIStoryboard(name: "Storyboard", bundle: nil)
    let myVC2 = storyboard.instantiateViewControllerWithIdentifier("navProfile") as UIViewController
    let myVC1 = storyboard.instantiateViewControllerWithIdentifier("navigation") as UIViewController
    let myVC3 = storyboard.instantiateViewControllerWithIdentifier("navComp") as UIViewController
    var controllers = [myVC1,myVC3]
    tabBarController.viewControllers = controllers
    self.window?.rootViewController = tabBarController
    var query = PFUser.query()
    return true
}

但是我的应用程序使用登录,当用户登录时,我希望显示标签栏控制器。这是我的登录视图控制器

@IBAction func SignIn(sender: AnyObject) {
    LoginViewController().showActivityIndicator(self.view)
    PFUser.logInWithUsernameInBackground(UsernameTextField.text,password: PasswordTextField.text){
        (user:PFUser!, error:NSError!)->Void in
        if user != nil{
            println("Login successfull")
            let storyboard = UIStoryboard(name: "Storyboard", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("navigation") as UIViewController
            self.presentViewController(vc, animated: false, completion: nil);

然而,这会链接到UINavigationController,标识符为“navigation” 有没有办法为Appdelegate中创建的标签栏控制器发送故事板标识符或以某种方式实例化到视图

1 个答案:

答案 0 :(得分:0)

开始处理类似的项目。我不知道这是否是最佳做法,但我会检查AppDelegate是否有登录用户,然后选择应该显示哪个视图。

这种方法的主要原因是,如果您无法在窗口层次结构中显示其视图的视图。这样您就不会让视图出现,然后呈现另一个视图,从而使过程更加顺畅。

希望这个服务

    var user = PFUser.currentUser()
    if user == nil {
        // Do stuff after successful login.
        println("Login Not Succesful")
    } else {
        // The login failed. Check error to see why.
        println("Login Succesful")
        var rootViewController = self.window!.rootViewController
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        var viewController = mainStoryboard.instantiateViewControllerWithIdentifier("tabbc") as! UITabBarController

        window?.rootViewController = viewController
        window?.makeKeyAndVisible()

    }