开发者

issue with custom tab bar and navigation controller

开发者 https://www.devze.com 2023-04-05 01:53 出处:网络
Ok, so I have the following: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

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:

issue with custom tab bar and navigation controller

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 (i.e. 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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号