单击相应的UIButton后如何转到UITabBarController

时间:2015-01-05 04:49:34

标签: ios objective-c storyboard uitabbarcontroller

how to go on home page on login button click

如何在登录按钮上的主页上单击

- (IBAction)loginClick:(id)sender {
    HomeVC *secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"home"];
    [self.navigationController pushViewController:secondView animated:YES];
    self.tabBarController.selectedIndex = 1;
}

2 个答案:

答案 0 :(得分:1)

首先,您必须将登录页面设置为Root控制器,当您成功登录时,您必须更改根控制器并将Tab Bar作为根控制器。 在AppDelegate中执行此代码,并在登录和注销页面上调用这些方法。

@property (strong, nonatomic) UIWindow *window;
@property(nonatomic,readonly) UITabBar *tabBar;
@property(nonatomic,strong) UITabBarController *tabBarController;


- (void)Login{

  [self.window setRootViewController:nil];
   UIStoryboard *MainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone"                                                             bundle: nil];
   UITabBarController *bar= (UITabBarController*)[MainStoryboard instantiateViewControllerWithIdentifier:@"tabbar"];
   self.window.rootViewController=bar;

}

 -(void)Logout
  {
   // self.window.rootViewController = nil;
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    UINavigationController*vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"homepage"];
   // Homepage *controller = [[Homepage alloc] initWithDelegate:self];
    self.window.rootViewController = vc;
  }

答案 1 :(得分:0)

以这种方式实现didFinishLaunching方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"LoginView"];

BOOL isLogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"IS_LOGIN"];
if (isLogin == YES)
{
    rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"MainHomeTabView"];
}
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];

return YES;
}

按下登录按钮

时设置为TRUE
- (IBAction)login:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"IS_LOGIN"];
[[NSUserDefaults standardUserDefaults] synchronize];

TabListViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainHomeTabView"];
AppDelegate *appDelagate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelagate.window.rootViewController = vc;
}