简体   繁体   中英

issue with custom tab bar and navigation controller

Ok, so I have the following:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    MainViewController * tabBarController = [[MainViewController alloc] init];
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];
    [tabBarController release];

    [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|
     UIRemoteNotificationTypeAlert|
     UIRemoteNotificationTypeSound];

    return YES;

}

Here, MainViewController is just a subclass of a UITabBarController, and inside MainViewController's viewDidLoad I have:

- (void)viewDidLoad
{
    [super viewDidLoad];

 NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:3];

MapViewController * map = [[MapViewController alloc] init];
    [localControllersArray addObject:map];

    //[localNavigationController release];
    [map release];


    ListViewController * voteSpot = [[ListViewController alloc] initWithTabBar];
    [localControllersArray addObject:voteSpot];

    //[localNavigationController release];
    [voteSpot release];


    ProfileViewController * profile = [[ProfileViewController alloc] initWithTabBar];
    [localControllersArray addObject:profile];

    //[localNavigationController release];
    [profile release];


    self.viewControllers = localControllersArray;
    [localControllersArray release];
}

and now what I can see is just:

在此处输入图片说明

Any idea why it is a white screen?

Here's an example of my initWithTabBar:

-(id) initWithTabBar {
    if ([self init]) {
        self.navigationItem.title=@"Map";
    }
    return self;
}

Ignore the bottom tab bar momentarily (middle one missing), that does exactly what I want.. What I am confused is with the viewController associated with each tab, it has nothing on it, while in fact MapViewController has a MapView in it. When I click on any tab then it will crash (program received signal: EXC_BAD_ACCESS) at int retVal = UIApplicationMain(...)

UPDATE:

If you want to debug it, I've uploaded a sample code at git hub where you can download the whole project (it's a simple test project, I promise)

You should be adding your controllers to the TabBarControllers viewControllers property. Like so:

self.viewControllers = [NSArray arrayWithObjects:map, voteSpot, profile, nil];

Edit: Sorry, I didn't see that you already had that. However, depending on the actual problem, the above snippet could actually solve your problem.

A few things:

  1. I can't see the creation of your localControllersArray. Is it autoreleased or not?
  2. The error you're getting indicates a memory problem (ie accessing a variable that has been freed). You can set NSZombieEnabled = YES in the build scheme to find exactly which variable is causing the problem.
  3. I personally like to create the view controllers in the app delegate and assign them there. There's no reason (that I'm aware of) that it shouldn't work in viewDidLoad, though.

Edit 2: After looking at your project, I was able to get it up and running and showing your tab views by changing the applicationDidFinishLaunchingWithOptions method to look like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    BaseViewController * tabBarController = [[BaseViewController alloc] init];

    NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:3];   

    //MapViewController * map = [[MapViewController alloc] init];
    //UINavigationController* mapNavController = [[[UINavigationController alloc]
    //                                              initWithRootViewController:map] autorelease];
    //[map release];
    //[localControllersArray addObject:mapNavController];


    ProfileViewController * profile = [[ProfileViewController alloc] init];
    [localControllersArray addObject:profile];
    [profile release];

    tabBarController.viewControllers = localControllersArray;
    [localControllersArray release];

    self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];
    [tabBarController release];
    return YES;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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