Malloc
Malloc

Reputation: 16276

calling to a protocol method in a navigation stack

I declared a protocol method in order to be called by its delegate. This is the relevant code:

The view where the protocol is delared:

CategoryViewController.h

@class  CategoryViewController;
@protocol CategoryViewControllerDelegate<NSObject>
-(void)loadProductsList:(id)sender;


@end

@interface CategoryViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{

    id delegate;

}

@property(nonatomic, strong)id <CategoryViewControllerDelegate>delegate;

CategoryViewController.m

@implementation CategoryViewController

@synthesize delegate;

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
            CategoryViewController *catView = [[CategoryViewController alloc] initWithNibName:@"CategoryViewController" bundle:nil];
            [self.navigationController pushViewController:catView animated:YES];

            if([self.delegate respondsToSelector:@selector(loadProductsList:)]){
                [self.delegate loadProductsList:[arrayCategory objectAtIndex:indexPath.row]];
            }
    }

The delegate view is called MainViewController, in the viewDidLoad method of MainViewController, I set the delegate to self:

-(void)viewDidLoad{
    //Use a property of CategoryViewController to set the delegate

    self.categoryController.delegate = self;
}

-(void)loadProductsList:(id)sender{
//Logic


}

Let me explain to you, CategoryViewController is managed by a UINavigationController so when click on a cell, I create a new instance of CategoryViewController and push it to the navigation stack. Then I call to the protocol method:

if([self.delegate respondsToSelector:@selector(loadProductsList:)]){
  [self.delegate loadProductsList:[arrayCategory objectAtIndex:indexPath.row]];
    }

The problem is that the delegate is only valid for the root view, when the CategoryViewController present view is 0 index. Then the delegate is null and so the protocol method loadProductsList:cannot be fired when I try to call it from stack view index 1, 2, etc. When I go back to index 0 (root view in the navigation stack) the delegate object is valid again and I can call the protocol method.

My question is:

Why I cannot fire the protocol method after I create a new instance of CategoryViewController and push it to the navigation stack? Why the delegate object gets null then? Thanx in advance.

Upvotes: 1

Views: 129

Answers (1)

pdrcabrod
pdrcabrod

Reputation: 1477

You set the delegate only for one (the first) of your CategoryViewController classes.

Every time a row is selected you are creating a new CategoryViewController class whose delegate is nil since you havent set it up.

Edit,

I see two options here.

a) You can do you MainController a singleton, so you can access it from any point in your code. Then you would be able to set it in the didSelectRowAtIndexPath as a delegate.

b) Yo can recusively pass the delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        CategoryViewController *catView = [[CategoryViewController alloc] initWithNibName:@"CategoryViewController" bundle:nil];
        [self.navigationController pushViewController:catView animated:YES];

        catView.delegate = self.delegate;

        if([self.delegate respondsToSelector:@selector(loadProductsList:)]){
            [self.delegate loadProductsList:[arrayCategory objectAtIndex:indexPath.row]];
        }
}

Upvotes: 2

Related Questions