PAcan
PAcan

Reputation: 869

Most efficient way to show another ViewController

I have an app that has a main view controller (login controller). If the authorization is completed I will show another controller(tabBarController) to the user. I've made it in this way:

AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    LogInViewController *logInController = [[[LogInViewController alloc] init] autorelease];

    _tabBarController = [[UITabBarController alloc] init] ;

    SearchViewController *searchController = [[[SearchViewController alloc] init] autorelease];
    CabinetViewController *cabinetController = [[[CabinetViewController alloc] init] autorelease];
    HelpViewController *helpController = [[[HelpViewController alloc] init] autorelease];
    CatalogViewController *catalogController = [[[CatalogViewController alloc] init] autorelease];    

    NSArray *controllerArray = [NSArray arrayWithObjects:cabinetController, catalogController, searchController, helpController, nil];

    [_tabBarController setViewControllers:controllerArray];

    //[navController setViewControllers:controllerArray];
    [self.window setRootViewController:logInController];
    [self.window makeKeyAndVisible];
    return YES;
}

And then on the button event I've made this:

-(void)logInUser:(id)sender
{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [delegate.window setRootViewController:delegate.tabBarController];
    [delegate.tabBarController setSelectedIndex:0];
    [self.view removeFromSuperview];
}

How to do this kind of stuff in more efficient way??? Thanks everyone in advance.

Upvotes: 1

Views: 128

Answers (2)

aethe
aethe

Reputation: 909

Since you're asking what's the most efficient way, that's easy. Use Storyboards.

This is literally exactly the problem Storyboards were designed to solve. You can create all your view controllers, set many of the desired properties - including the fact that you want the outer Navigation Controller to hide its navigation bar - in the storyboard itself and you'll get a realtime editor that you can bounce ideas off. The best part? You can do everything you described in your question using Storyboards in zero lines of code. So that's zero lines of code to write and maintain. True story. I'll prove it!

I created an imitation of what you describe in your question in the storyboard below: Storyboard

This results in the following application in the simulator:

iOS Simulator

The best part, this is a screenshot of the entirety of the code in this application:

App Delegate

I don't think it gets much more efficient than this. If you're new to storyboards I highly recommend reading the fantastic beginner's guide from Ray Wenderlich, Beginning Storyboards in iOS 5.

Upvotes: 3

Jitendra
Jitendra

Reputation: 5081

- (IBAction)HelpClicked:(id)sender
{

     _tabBarController = [[UITabBarController alloc]initWithNibName:@"UITabBarController" bundle:nil]; ;

    [self.navigationController pushViewController:_tabBarController animated:YES];

}

try this one...

Upvotes: 0

Related Questions