views:

219

answers:

2

I've programmatically created a UITabBarController that is loaded in my App Delegate like this:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
tabBarController = [[UITabBarController alloc] init];

myTableViewController = [[MyTableViewController alloc] init];
UINavigationController *tableNavController = [[[UINavigationController alloc] initWithRootViewController:myTableViewController] autorelease];
myTableViewController.title = @"Tab 1";
[myTableViewController release];

mySecondTableViewController = [[MySecondTableViewController alloc] init];
UINavigationController *table2NavController = [[[UINavigationController alloc] initWithRootViewController:mySecondTableViewController] autorelease];
mySecondTableViewController.title = @"Tab 2";
[mySecondTableViewController release];

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

[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}

Now the issue I have is that I can get into the views no problem, but when I try and click onto any item in the Table View, I can't get a secondary table view to appear in any tab. The tabs work absolutely fine, just the secondary views. I'm using the code below in my myTableViewController to run when selecting a specific row (the code reaches the HELP line, and crashes)...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
    // this gets the correct view controller from a list of controllers
SecondaryViewController *svc = [self.controllers objectAtIndex:row];

    /*** HELP NEEDED WITH THIS LINE ***/    
[self.navigationController pushViewController:svc animated:YES];

}

Simply put, I'm trying to switch views to the new view controller whilst keeping the tabs available and using the navigation to go back and forth (like in the iTunes App).

Any help appreciated.

Thanks

A: 

You've not included in your question how you initialize the self.controllers array.

I suspect this array is not filled with initialized SecondaryViewController objects.

EDIT (Added Code example that works for me):

the .h file:

@interface FirstLevelViewController : UITableViewController {
    NSArray *controllers;
}
@property (nonatomic, retain) NSArray *controllers;
@end

and the .m file:

@implementation FirstLevelViewController
@synthesize controllers;
- (void)viewDidLoad {
    self.title = @"First Level";
    NSMutableArray *array = [[NSMutableArray alloc] init];

    // Disclosure Button
    DisclosureButtonController *disclosureButtonController =
    [[DisclosureButtonController alloc] 
     initWithStyle:UITableViewStylePlain];
    disclosureButtonController.title = @"Disclosure Buttons";
    disclosureButtonController.rowImage = [UIImage 
                                           imageNamed:@"disclosureButtonControllerIcon.png"];
    [array addObject:disclosureButtonController];
    [disclosureButtonController release];  

    // deleted further adds to array ...

    self.controllers = array;
    [array release];
    [super viewDidLoad];
}
Frank Martin
These are initialized SecondaryViewController objects... ... in MyTableViewController viewDidLoad I have: SecondaryViewController *svc = [[SecondaryViewController alloc] initWithStyle:UITableViewStylePlain]; svc.title = @"svc"; [array addObject:svc]; [svc release]; self.controllers = array;and this object is returned when I get the controller from the self.controllers array. The value is not nil (so it's created) and the title is set correctly. Just FYI SecondaryViewController subclasses UITableViewController and self-delegates
Paul Johnston
Hmmm ... seems we have the same book in front of us (Beginning iPhone 3 Development) ;-)I can't spot any errors in your code. Are there any error messages from the compiler?
Frank Martin
I've just tried to run the code by creating the svc object directly (instead of pulling it out of an array) and it works... so there's something with the pulling of the object from the array that's not quite right
Paul Johnston
I just checked the source code belonging to the book if there were any corrections or errata. They do it exactly the way you describe it, and it compiles and runs fine in my simulator.Do you get any messages in the debugger window during the failure?
Frank Martin
Sorry... had to go out for a bit.Hmm... glad I'm going in the right direction, just really confused as to why it just doesn't work out of the box! Creating the object directly as opposed to getting the object from an array seems to work. I'll work on that to see if I can fix it.
Paul Johnston
Have found that I'd initialised the objects, put them into the array and then released them. Not releasing them makes it work... once! The second time you click it dies (again!)...I'm soooo confused
Paul Johnston
How do you setup the controllers array? @property (nonatomic, retain) NSArray *controllers; in the .h, and then @synthesize controllers; in .m? Once the Controller object is added to the array the array should retain the controller object as far as i understood that whole memory management stuff.
Frank Martin
I added a working code example to my answer. Maybe you find by comparison why your code is crashing.
Frank Martin
A: 

Simply put, I was creating the controller array, self.controllers, and adding objects, and then releasing the objects.

If you do not release the object until the array is released, it appears to work no problem.

Paul Johnston
Uuups - i haven't noticed that you answered your question. Is your problem solved?
Frank Martin