0

I'm working on a navigation-based application. My rootViewController contains 3 cells -When the first one is pressed a UIViewController is pushed (THIS WORKS) -The problem is with the 2nd and 3rd cells that are supposed to push a UITableViewController

The app is running with no errors and it is NOT crashing at all, but when i navigate to the tableview, an empty table is viewed with no elements in it.

Is the problem with this part of the code?? :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:@"Introduction" bundle:nil];
    detailViewController.title=@"Introduction";

    UITableViewController *myPictures = [[UITableViewController alloc] initWithNibName:@"Pictures" bundle:nil];
    myPictures.title=@"Pictures";

    UITableViewController *myImages = [[UITableViewController alloc] initWithNibName:@"ImagesViewController" bundle:nil];
    myImages.title=@"Images";

    // Pass the selected object to the new view controller.
    if (0 == indexPath.row)
    [self.navigationController pushViewController:detailViewController animated:YES];

    if (1== indexPath.row)
    [self.navigationController pushViewController:myPictures animated:YES];

    if (2== indexPath.row)
    [self.navigationController pushViewController:myImages animated:YES];

    [detailViewController release];
    [myPictures release];   
    [myImages release];
Dani A
  • 3
  • 1
  • 1
    You should create the instances of your viewControllers only if the according row has been selected to minimize your memory footprint. Can you explain what you mean by "but when i navigate to the tableview"? Do you mean, by clicking on the cell which should show the viewController? – Nick Weaver May 01 '11 at 13:00

1 Answers1

0

What you're doing it very wrong (apart from your original problem). Why are you instantiating each view controller and then only using the one based on the current 'cell selection'? This will slow your app down depending on how much time it would take to load each of these separate views. You should only instantiate the relevant view controller inside the "if (indexPath.row == 2) {" block.

Apart from that there are many things wrong about your approach. What you are doing will never work since instantiating a generic UITableViewController (even if you supply your own nib) will obviously only show you an empty view. You need to have a class of your own which the nib is tied to as a delegate which will then supply the TableView with data.

I believe when you created these nib files (for example "Pictures") xcode also give you a 'PicturesViewController.h and PicturesViewController.m" files? If so, you need to write the appropriate code there and ensure the Tableview in the "Pictures" nib file has its 'datasource' and 'delegate' set to 'PicturesViewController'. Then when you want to show that view do this instead:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == 1) {
        ...
    } else if (indexPath.row == 2) {
     PicturesViewController *myPictures = [[PicturesViewController alloc] initWithNibName:@"Pictures" bundle:nil];

     [self.navigationController pushViewController:myPictures animated:YES];
     [myPictures release];
    }

} 
strange
  • 9,654
  • 6
  • 33
  • 47
  • thanks for the reply, well, i corrected my code + in my picturesviewcontroller.h &.m i used an array of the items to display and the tableview in the "pictures" nib file has its 'datasource' and 'delegate' set to 'picturesviewcontroller'. but your code is giving an error "unexpected interface pictureViewController" how can i do that: "You need to have a class of your own which the nib is tied to as a delegate which will then supply the TableView with data." – Dani A May 01 '11 at 13:28
  • 1
    Without looking at your code I can't say. However that simply means you've named your class differently and you're using my code verbatim which won't work. That was just to serve as an example. Please replace all class names in my code with actual names you're using. Also an 'acceptance' of the answer would be appreciated. – strange May 02 '11 at 13:07
  • lool.. i'm still new at the iphone dev thingy but i'm not that stupid to use the same names of classes u used as an example :P – Dani A May 03 '11 at 13:09