0

I'm really not sure how to express this question in the fewest and clearest words possible. But I'll try my best.

I have a class ShoppingCartVC and I want to add products to it. So I modally present a CategoriesVC modally. When I select a category in the tableView row, it segues to a ProductsVC containing all the products in that category. So now I can select a product. But how do I send that selected object back to ShoppingCartVC? I was able to successfully implement this before using delegation but that was when I didn't have the CategoriesVC. I just segue directly to the ProductsVC so before I segue, I can set ShoppingCartVC(the presenting VC) as the delegate of ProductsVC and dismiss it whenever a product is selected.

But now since ProductsVC is 1VC further down the VC hierarchy in my navigationController, I cannot do that.

I tried searching about NSNotification but that doesn't seem to be the right solution.

How do I solve this? Hopefully you can give me some sample codes.

acecapades
  • 893
  • 12
  • 23
  • I tried searching the entire stackoverflow for this question for the past 2 days already but I cannot find and answer.. :) – acecapades May 15 '12 at 03:26
  • I have the same question as this guy (http://stackoverflow.com/questions/7955309/delegation-from-a-nested-navigation-stack) but no one has answered him yet. – acecapades May 15 '12 at 03:29
  • i think u can use NSUserdefaults – Rohan May 15 '12 at 04:35

2 Answers2

1

Maybe I am missing something, but what is the problem with passing the reference to ShoppingCartVC along from CategoriesVC to ProductsVC? You should be able to accomplish what you are looking for using a delegate pattern or posting an NSNotification that the ShoppingCartVC is listening for.

Another way to do it would be to create a shopping cart singleton (NSObject with a shopping cart array property that holds each product) that you could add items to from anywhere, and then when your ShoppingCartVC appears, update the contents of the cart you are displaying with the current contents of the singleton object.

Joel
  • 15,654
  • 5
  • 37
  • 60
1

I think delegate pattern is the best solution for your problem.

There are 3 ViewControllers in this case :

  1. ShoppingCartViewController
  2. CategoryViewController
  3. ProductViewController

ShoppingCartViewController gets category from CategoryViewController.

ShoppingCartViewController gets product from ProductViewController.

Solutions :

  • Create protocols CategoryViewControllerDelegate and ProductViewControllerDelegate.

CategoryViewControllerDelegate

@protocol CategoryViewControllerDelegate <NSObject>
...
- (void)categoryViewController:(CategoryViewController *)categoryViewController didSelectCategoryAtIndex:(int)index;
...
@end

ProductViewControllerDelegate

@protocol ProductViewControllerDelegate <NSObject>
...
- (void)productViewController:(ProductViewController *)productViewController didSelectCategoryAtIndex:(int)index;
...
@end
  • Implement protocols in ShoppingCartViewController and show UINavigationController as modal with CategoryViewController as rootViewController.

  • Get selected category from categoryViewController:didSelectCategoryAtIndex: and push productViewController to navigationController.

Push productViewController to navigationController

ProductViewController *productViewController = [ProductViewController new];
productViewController.delegate = self;
[categoryViewController.navigationViewController pushViewController:productViewController animated:YES];

There you get both category and product in ShoppingCartViewController.

Willy
  • 9,681
  • 5
  • 26
  • 25