所以在我的AppDelegate中,我已经声明了我的标签控制器和视图等 但是,如果没有用户登录,则代码在登录时执行,但是一旦用户成功登录,我需要再次执行。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
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,myVC2]
tabBarController.viewControllers = controllers
self.window?.rootViewController = tabBarController
if PFUser.currentUser() != nil{
var query = PFUser.query()
query.whereKey("objectId", equalTo: PFUser.currentUser().objectId)
query.findObjectsInBackgroundWithBlock {
(results: [AnyObject]!, error: NSError!) -> Void in
for listing in results {
var usertype = listing["UserType"] as? String
if usertype == "Student"{
var controllers = [myVC1,myVC2]
tabBarController.viewControllers = controllers
self.window?.rootViewController = tabBarController
}
else if usertype == "Employer" {
var controllers = [myVC1,myVC3]
tabBarController.viewControllers = controllers
self.window?.rootViewController = tabBarController
}
}
}
}
答案 0 :(得分:2)
只需制作一个说明用户已登录的bool。 为例
var userIsLoggedIn : bool
您将其设置为true并登录,然后在代码
中使用if答案 1 :(得分:0)
您可能希望从登录检查开始。如果没有人登录,则从您的应用代表提供登录屏幕。如果有人已登录,则显示正常的控制器层次结构。
因此,在您的app委托中,您需要2个方法:一个创建登录控制器,另一个创建正常层次结构。通过更改窗口rootViewController在两者之间切换。最简单的机制是在您的app委托中提供一种方法,为您执行此操作:例如showLoginController和showLoggedInController。您可以将逻辑放在这些逻辑中,以处理设置给定用户数据以供显示。
登录控制器现在可以在登录发生时请求切换,并且正常登录的控制器可以请求在用户退出时返回登录屏幕:全部通过应用程序委托实例。
答案 2 :(得分:0)
您可以在loginVC
和nextVCs
之间发布解决方案(多个标签栏控制器)
将Tabview放在storyboard
中,并连接所有类,如myVC1,myVC2,myVC3,
只需子视图标签栏类,然后为storyboard
TabBatController
分配课程。
在子文件- (void)viewDidLoad
中,您可以验证此代码并动态加载视图,
- (void)viewDidLoad
{
let tabbar = self.tabBarController?.tabBar
if PFUser.currentUser() != nil{
var query = PFUser.query()
query.whereKey("objectId", equalTo: PFUser.currentUser().objectId)
query.findObjectsInBackgroundWithBlock {
(results: [AnyObject]!, error: NSError!) -> Void in
for listing in results {
var usertype = listing["UserType"] as? String
if usertype == "Student"{
let controlrs = self.setViewControllers
controlrs.removeObjectAtIndex(2)
}
else if usertype == "Employer" {
let controlrs = self.setViewControllers
controlrs.removeObjectAtIndex(1)
}
}
tabbar .setViewControllers(controlrs, animated: NO);
}
}
}
HTH,享受编码!!