0

So I have a ViewController class called "TopNewsViewController". It gets pushed onto the NavigationController stack from RootViewController.

In TopNewsViewController, I have a pop over that appears by clicking a button. It displays a Table View from PopOver.

Now, in the method tableView:didSelectRowAtIndexPath in PopOver, I want it to modify some properties of TopNewsViewController and re-push it to the navigation stack. I am implementing the method as follows:

TopNewsViewController *topNewsViewController = [[[TopNewsViewController alloc] initWithNibName:@"TopNewsViewController" bundle:nil] autorelease];

NSString *feedStr = [rootViewController.feeds objectAtIndex:rowNumber];
[rootViewController release];
NSArray *thisFeed = [NSArray arrayWithObjects:feedStr, nil];
topNewsViewController.feeds = thisFeed;
topNewsViewController.pageTitle = [categories objectAtIndex:rowNumber];
[self.navigationController pushViewController:topNewsViewController animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];

I realized that this does not work because I am using "self.navigationController", when PopOver is not a ViewController, and instead is a UITableViewController. I tried doing "topNewsViewController.navigationController" instead but it did not work.

Anyone has any suggestions on how to implement it? Help/suggestions are greatly appreciated!

darksky
  • 20,411
  • 61
  • 165
  • 254

1 Answers1

1

If I'm understanding you correctly, I think probably your best bet would be to implement a delegate in your popover's UITableViewController, such that when a record is selected, it calls the delegate method to hand back the selection to your RootViewController. The RootViewController would then handle pushing and popping whatever new TopNewsViewControllers you want to use. The new TopNewsViewController you are creating here doesn't have a nav controller by default, that's why it won't work.

So, your root view controller becomes the delegate of your popover view controller... when a row is selected, it says "popOver:didSelectRow:" and then RootViewController dismisses the popover and "does the right thing"(tm).

Jiva DeVoe
  • 1,338
  • 1
  • 9
  • 10
  • Oh - you are right. But how would I implement my RootViewController as a "delegate"? Just instantiate a RootViewController object in my popover view controller? Like that: RootViewController *rootViewController = [[RootViewController alloc] initWithNib....? – darksky Apr 14 '11 at 16:44
  • See: [How to create delegates in Objective-C](http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c) – Jiva DeVoe Apr 14 '11 at 16:50
  • So I created this in popoverviewcontroller: `@protocol PopOverDelegate @required - (void)didSelectRow:(NSUInteger)row; @end` and in tableView:didSelectRowAtIndexPath: in the implementation I have: `NSUInteger rowNumber = [indexPath row]; [[self delegate] didSelectRow:rowNumber]; [tableView deselectRowAtIndexPath:indexPath animated:YES];` But didSelectRow is not getting invoked (did some debugging). Am I creating the protocol incorrectly? – darksky Apr 15 '11 at 02:12
  • Did you set your root view controller as the delegate of the popover table view controller? – Jiva DeVoe Apr 15 '11 at 07:56
  • I've been trying to do that. I know its something.delegate = self. But where do I put that? Would it be in RootviewController? Should I create an instance of a popover table view controller like PopOverViewController *popOverViewController = [PopOverViewController alloc] etc.... and then popOverViewController.delegate = self; in the ViewDidLoad of Root ViewController? – darksky Apr 16 '11 at 19:40
  • Yes, wherever you create your popoverviewcontroller, after creating it, set your delegate to your rootViewController by doing controller.delegate = self; or something akin to that. – Jiva DeVoe Apr 17 '11 at 03:40
  • I already did that in the ViewDidLoad of RootViewController and it's not working... I'm pretty sure my delegate is set up correctly though. – darksky Apr 17 '11 at 15:11