User delegate returns nil

I am creating such a protocol.

   @protocol parsingComplete <NSObject>
   @optional
     -(void) updateUI:(NSMutableDictionary*)foodList;
   @end

   @interface foodParser:NSObject<NSXMLParserDelegate>

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

After the parsing is complete, I want this delegate to start. so I am doing something like this.

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    if (delegate) 
    {
        [delegate updateUI:food];
    }
 }

Here is the delegate value nil. Anyone understands the source of this problem.

And I call my delegate as follows. here .h file {

 @interface NHMainViewController : UIViewController<parsingComplete>

 @property(nonatomic, strong)ATAFoodParser *foodParser;
 @end

}

here .m file

{

@implementation NHMainViewController

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
       // Custom initialization
      }
      return self;
}

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


-(void) updateUI:(NSMutableDictionary*)foodList{
       NSLog(@"Dictionary:---->%@", foodList);
 }

@end

}

updateUI is my delegation method that needs to be called. I do not call here. I went to my first class, where I created my protocol, I typed a delegate .. it's zero.

+4
source share
2 answers

Your problem is that you do not save your " foodParser" as a property or instance variable:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    ATAFoodParser *foodParser = [[ATAFoodParser alloc] init]; 
    foodParser.delegate = self; 
}

"foodParser" - , ( ARC), "viewDidLoad".

, NH.MainViewController.h.

@property (strong) ATAFoodParser * foodparser;

"viewDidLoad:" :

self.foodParser = [[ATAFoodParser alloc] init]; 
self.foodparser.delegate = self;

.

+4

, .

delegate, .

1) .h , @interface FirstViewController : UIViewController<parsingComplete>

.m file.

2), , secondviewController, ,

SecondViewController *second = [[SecondViewController alloc] init];
second.delegate = self;

. . , secondView firstView , .

: , stack .

0

All Articles