0

I have a ViewBased App. I added a UITableView on one of the UIViewControllers. It shows the data and I implemented all the delegate methods etc. My problem is when I want to show the detailView it just doesn't happen. My code:

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
       DetailViewController *detailViewController =[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

       NSLog(@"DidSelectRowAtIndexPath");

        // Pass the selected object to the new view controller.
        [self.navigationController pushViewController:detailViewController animated:YES];
        [detailViewController release];
    }

I see that I need a navigationController but I don't have one and I was unsucessful trying to add one programatically. I don't have one in my appDelegate either, so my question is do I need to add one to show the detail view? If yes, please give me a code sample how to do that. If not, what other way is there?

I am new to iOS so I am a bit lost here. Please help!

iPhoneNoob
  • 173
  • 1
  • 6
  • 19

3 Answers3

1

To add a navigation controller programmatically just for this detail view, you need to something like this:

UINavigationController * controller = [[UINavigationController alloc] initWithRootViewController:detailViewController];

[[detailViewController] release];

[self presentModalViewController: controller animated: YES];

If you want to use pushViewController, you need to already have a navigation controller surrounding the view you're starting with.

Alex Gosselin
  • 2,942
  • 21
  • 37
  • Thanks a lot! That helps me! Is there a way to push it from right to left like in the samples, using presentModalViewController? – iPhoneNoob Feb 24 '12 at 23:05
  • You might find help for something like this here: http://stackoverflow.com/questions/237310/how-can-i-change-the-animation-style-of-a-modal-uiviewcontroller – Alex Gosselin Feb 24 '12 at 23:08
0

You need to add the Navigation Controller FIRST, then your master table becomes the root view controller of the nav controller, then when you tap a row in the table, you push another view controller onto the nav stack.

How does your master table get into the app in the first place? If you're using a nib, it's super easy to just change out the view controller for a nav controller with the old view controller added as a child of the nav controller.

jsd
  • 7,673
  • 5
  • 27
  • 47
  • Can you tell me where and how to add a navC? I tried to add it (see my code) but it did not work. I am not using UITableViewController and did not add a navC in my appDelegate. I am using a nib and tried adding a navC but didn't know how to link it all together... :-/ – iPhoneNoob Feb 24 '12 at 23:31
  • Is the master the UITableViewController? How does that UITableViewController get displayed in the first place? – jsd Feb 25 '12 at 04:30
0

You can create one programmatically by working within your app delegate's application:didFinishLaunchingWithOptions: method like so:

UITableViewController *tableViewController = [[[WhateverYourSubclassVCIsCalled alloc] init] autorelease];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
window.rootViewController = navController;
[window makeKeyAndVisible];
lyricsboy
  • 2,999
  • 24
  • 21
  • I am not using a UITableViewController. I added the delegates to my ViewController. And I don't have a 'window'. It's all in the viewControllers. Is there any way to add a navControler to the VC without going through the appDelegate? – iPhoneNoob Feb 24 '12 at 23:28
  • Well, in that case, just use whatever your subclass name is in place of UITableViewController. And you do have a window, whether you actually have a handle on it or not. I believe the current templates use the name `_window` for the instance variable in the app delegate. – lyricsboy Feb 24 '12 at 23:34
  • I think you are thinking about the relationship between a nav controller and other view controllers in reverse. A navigation controller is designed to contain other view controllers and manage the "stack" by pushing and popping them. So you want to have some class (typically the app delegate, or indirectly via the XIB) create and set up the nav controller with your own custom view controller as its root. – lyricsboy Feb 24 '12 at 23:37