简体   繁体   English

在标签栏控制器之前显示登录视图控制器

[英]Show login view controller before tab bar controller

I am new to iphone development. 我是iPhone开发的新手。 I am developing an iphone application which contains four tabs. 我正在开发一个包含四个选项卡的iphone应用程序。 I have implemented it using tab bar controller. 我已经使用标签栏控制器实现了它。 But now i need to show a login screen without tabs before tab bar controller. 但是现在我需要在选项卡栏控制器之前显示一个没有选项卡的登录屏幕。 I have tried so many methods but didnt get the one i wanted. 我尝试了很多方法,但没有得到我想要的方法。

Can anyone pls explain how to do this with a code snippet?? 谁能用代码片段解释如何做到这一点?

Create a new class LoginViewController. 创建一个新的类LoginViewController。 When your application launches then add the view to the window. 当您的应用程序启动时,然后将视图添加到窗口中。 Now when login is successful then remove it from superview and add the MainController. 现在,当登录成功后,将其从超级视图中删除并添加MainController。

Create a subclass of UITabBarController (though it's not advised by apple), but for this purposes it should be ok and do this in viewWillAppear 创建一个UITabBarController的子类(尽管苹果不建议这样做),但为此目的应该可以,并在viewWillAppear中进行此操作

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    BOOL isLogged in = //do something to determine if you're logged in
    if(!loggedIn){
        LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewControllerNibHere" bundle:nil];
        [self presentModalViewController:loginViewController animated:YES]; //or NO if you don't want it animated
        [loginViewController release];
    }
}

Or add this to a category for UITabBarController and import it in the app delegate or wherever you're using the UITabBarController 或将其添加到UITabBarController的类别中,并将其导入到应用程序委托中或使用UITabBarController的任何位置

The best way to do this is to create a new LoginViewController as other people have mentioned and then set your rootviewController to tabBarcontroller as soon as you authenticate the user. 最好的方法是创建一个新的LoginViewController,就像其他人提到的那样,然后在对用户进行身份验证后立即将rootviewController设置为tabBarcontroller。 Here is how you can do this in swift, this is snippet to put as soon you authenticate your user in LoginViewController 这是您快速执行此操作的方法,这是您在LoginViewController中对用户进行身份验证后的摘要

let tabBarController = self.storyboard?.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = tabBarController

Where TabBarController is the storyboard id of your tab bar controller. 其中TabBarController是标签栏控制器的情节提要ID。 It could be anything whatever name you have given it. 可以是任何名称,无论您使用什么名称。

  • Create a new UIViewController subclass with a nib that represents your login screen (I'll refer to it as SignInViewController). 用代表您的登录屏幕的笔尖创建一个新的UIViewController子类(我将其称为SignInViewController)。
  • Open your MainWindow.nib file and add a new UIViewController 打开您的MainWindow.nib文件并添加一个新的UIViewController
  • Set the new UIViewController's class type to SignInViewController 将新的UIViewController的类类型设置为SignInViewController
  • Set the UIWindow's rootViewController outlet to the new SignInViewController 将UIWindow的rootViewController出口设置为新的SignInViewController
  • Now create a new nib file and copy your existing UITabBarController to it (it's best to split nibs rather than having a single-mega nib) 现在创建一个新的笔尖文件,并将您现有的UITabBarController复制到该文件中(最好分割笔尖,而不是使用单个巨型笔尖)
  • Back in your MainWindow.xib change the existing UITabBarController's attributes to specify the nib name that you just created 返回MainWindow.xib,更改现有的UITabBarController的属性以指定刚创建的笔尖名称

Check this Link's source code, 检查此链接的源代码,

it uses Login Controller as modal view with 4 tabs 它使用Login Controller作为具有4个选项卡的模式视图

http://code.google.com/p/tweetero/source/checkout http://code.google.com/p/tweetero/source/checkout

Also i tried this way , 我也尝试过这种方式

in my first tab view - in viewDidAppear - i'll check Login = YES then 在我的第一个标签视图中-在viewDidAppear -我将检查Login = YES,然后

show the LoginController 显示LoginController

- [self.tabbarcontroller presentMOdalViewcontroller:LoginView animated:YES];

so every time u click on first tab - if u need to Login put a flag - check it & show Login View 因此,每次您单击第一个选项卡时-如果您需要登录,请添加一个标志-选中它并显示登录视图

Hope this Helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM