iMeMyself
iMeMyself

Reputation: 1649

Navigation Controller loading twice, how to resolve this?

my project contains navigation controller and segmented control (with separate view controller: segmentManagingViewController) programmatically and now i added a tab bar in IB..while calling tab bar controller and navigation controller ,segmentManagingViewController view getting loaded twice.. both in tab bar item1 and in first segment i have called segmentManagingViewController view ....

here is screen shot of my app

screen shot

and following is application didFinishLaunchingWithOptions method... please do help me out to resolve this ...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    databaseName = @"breadworks.sql";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];


    [self checkAndCreateDatabase];
    [self readBreadsFromDatabase];
    [self categoryFromDatabase];
    SegmentManagingViewController * segmentManagingViewController = [[SegmentManagingViewController alloc] init];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:segmentManagingViewController];
    [segmentManagingViewController release];

    [self.window addSubview:tabBarController.view];
    [window addSubview: navigationController.view];
    [window makeKeyAndVisible];


    return YES;
}

Upvotes: 0

Views: 960

Answers (2)

iMeMyself
iMeMyself

Reputation: 1649

I declared navigation controller and view controllers (which are used as objects of NSArray) in delegate and created initWithNibName Constructor for view controllers( defining Title, Image and other properties of TabBarItems).. here is the updated code chunks ..

UIViewController *viewController1 = [[AtoZSecondviewController alloc] initWithNibName:@"AtoZSecondviewController" bundle:nil];
UIViewController *viewController2 = [[CategorySecondViewController alloc] initWithNibName:@"CategorySecondViewController" bundle:nil];

  tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController,viewController1 ,viewController2, nil];  

following is Definition of initWithNibName in ViewControllers

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self) {
        self.title = NSLocalizedString(@"Catogaries", @"Catogaries");
        self.tabBarItem.image = [UIImage imageNamed:@"TodaysChoice"];
    }
    return self;
}

Upvotes: 1

Lorenzo B
Lorenzo B

Reputation: 33428

Your code sounds strange to me.

First, since you use an UITabBarController set it as the rootViewController for your window.

Then, set the UINavigationController as a child controller of your tab bar controller.

Finally, as you did, set the rootViewController for your UINavigationController to segmentManagingViewController.

Now, since I prefer to do it without xib you could do the following.

UITabBarController* tabBarController = [[UITabBarController alloc] init];

SegmentManagingViewController * segmentManagingViewController = [[SegmentManagingViewController alloc] init];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:segmentManagingViewController];

tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController, nil];    

self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];

//- only if you don't use ARC -----
[segmentManagingViewController release];
[navigationController release];
[tabBarController release];
//----------------------------------

return YES;

If don't use ARC make attention to memory management!!

Hope it helps.

Upvotes: 1

Related Questions