简体   繁体   中英

Displaying a new view when selecting a row in a UITableView

I have an application with multiple views. I can click on different buttons to select different views, one of which displays a table. When I select a cell on that row, I want to display a new view. I see a log line indicating that view was loaded, but the screen doesn't change.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *targetCustomCell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

  [tableView deselectRowAtIndexPath:indexPath animated:YES];
  NSLog(@"in didSelectRowAtIndexPath");

  ItemDetailsViewController *detailsController = [[ItemDetailsViewController alloc] initWithNibName:@"ItemDetailsView" bundle:nil];
  [self.navigationController pushViewController:detailsController animated:YES];
  [self.window addSubview:[detailsController view]];
  [detailsController release];
  detailsController = nil;
}

In the ItemDetailsViewController, I have

- (void)viewDidLoad {
  [super viewDidLoad];
  NSLog(@"in ItemDetailsViewController.viewDidLoad ");
}

I see both log lines, but the screen doesn't change. Why isn't the view changing? Is there some other way to change the view?

What happens if you try to present your details controller using [self presentModalViewController: detailsController animated:YES] instead of the navigation controller's pushViewController? Also, adding the view to the window shouldn't be necessary. Pushing the view controller should be sufficient.

Try adding NSLog([self.navigationController description]) and see if self.navigationController is nil. (Might happen if your current controller isn't a sub-controller of a UINavigationController). Passing a message to a nil object just silently fails, so there's a good chance that's the problem.

Hope that helps!

EDIT: It's usually not necessary to subclass UINavigationController. Check out this page in Apple's docs: Apple Docs: Using Navigation Controllers It explains the setup pretty well. If you're creating your "root" view controller programatically in your application delegate, you'd want to do something like this:

// set up main view controller
DrawingBrowserController *controller = [[DrawingBrowserController alloc] init];

// create a navigation controller that "wraps" the controller
navigationController = [[UINavigationController alloc] initWithRootViewController: controller];

[controller release];

// Add the navigation controller's view to the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];

Basically, you're "wrapping" your root view controller in a navigation controller. If you're using Interface Builder, you can also set things up there.

Did you make sure your navigation controller is the root view of your window? You usually should do it like this in the applicationDidFinishLaunching method:

[window addSubview:navigationController.view];    
[window makeKeyAndVisible];

Additionally, you should remove [self.window addSubview:[detailsController view]]; , as it is not necessary.

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