简体   繁体   中英

Delegate Method is not getting called

I am trying to pass selected cell text from CategoryViewController to DescribeViewController. But it does not call the method in the DescribeViewController method.

CategoryViewController.h

#import <UIKit/UIKit.h>
@protocol CategoryViewControllerDelegate <NSObject>

- (void)didSelectRow:(NSString *)cellDataString;

@end

@interface CategoryViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) id<CategoryViewControllerDelegate> delegate;

@end

CategoryViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [categoryTableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = cell.textLabel.text;
    [self.delegate didSelectRow:cellText];
    [[self navigationController] popViewControllerAnimated:YES];
}

DescribeViewController.h

#import <UIKit/UIKit.h>
#import "CategoryViewController.h"

@interface DescribeViewController : ProductAwareBaseViewController<UITextFieldDelegate, CategoryViewControllerDelegate>

The following didSelectRow method is not getting called. I could not able to find out the root of the problem.

DescribeViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.        
    CategoryViewController *popoverTableViewController = [[CategoryViewController alloc] init];
    popoverTableViewController.delegate = self;

}

- (void)didSelectRow:(NSString *)cellDataString
{
    self.cellDataString = cellDataString;
}

ProductAwareBaseViewController.h

@import UIKit;

@class Product;

@interface ProductAwareBaseViewController : UIViewController
@property (nonatomic, strong) Product *product;
@end

ProductAwareBaseViewController.m

#import "ProductAwareBaseViewController.h"
#import "Product.h"

@interface ProductAwareBaseViewController ()

@end

@implementation ProductAwareBaseViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.destinationViewController isKindOfClass:[ProductAwareBaseViewController class]]) {
        ProductAwareBaseViewController *vc = (ProductAwareBaseViewController *)segue.destinationViewController;
        vc.product = self.product;
    }
}
@end

try

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

for declaring your delegate.

  1. EDITED

try for checking the delegate is returning some value or not. By this, whenever yo are setting the values.

if(self.delegate && [self.delegate respondsToSelector:@selector(didSelectRow:)])
    {
        [self.delegate didSelectRow:(NSString *) cellDataString];
    }

and also if you are using the segue to transfer the data between two controllers then check there for the delegates.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DescribeViewController *obj = (DescribeViewController *)segue.destinationViewController;
obj.delegate =self;
}

i think this will help you.

Try setting the delegate object in didSelectRow. And call that delegate method after that. Because delegate is weak, may be it is released from the memory.

CategoryViewController *popoverTableViewController = [[CategoryViewController alloc] init];
    popoverTableViewController.delegate = self;

UITableViewCell *cell = [categoryTableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = cell.textLabel.text;
    [self.delegate didSelectRow:cellText];
    [[self navigationController] popViewControllerAnimated:YES];

Most common reason for delegate method not being called is dealing with incorrect objects.

  1. Ensure that CategoryViewController object created from DescribeViewController is the same which you are presenting on screen and that the delegate is being set on the same object. I truly believe you are creating a new CategoryViewController object and setting delegate on that.
  2. In DescribeViewController , before calling delegate, check the existence of delegate and that it implements the protocol method (if its an optional method). This is a safety check, you can also put a NSLog statement to double check if your delegate exists or not. You are failing here.

->

if (delegate && [delegate respondsToSelector:(didSelectRow:)]) {
     [self.delegate didSelectRow:cellText];
}

PS: If you are segueing from DescribeViewController to CategoryViewController then you set delegate in prepareForSegue: .

Follow these guidelines and I am sure you would be able to fix your issue!

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