4

I am creating a protocol like this.

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

   @interface foodParser:NSObject<NSXMLParserDelegate>

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

After parsing complete I want this delegate to trigger. so i am doing something like this.

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

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

And i am invoking my delegate like this. here is .h file {

 @interface NHMainViewController : UIViewController<parsingComplete>

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

}

here is .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 a my delegate method, which should get invoked. i am not getting call back here. i went to my first class where i have created my protocol, i printed delegate.. it is nil..

Saif
  • 812
  • 1
  • 9
  • 18
  • how and where do you set the delegate? – Michael Dautermann Dec 28 '13 at 05:14
  • Where did you assign that delegate? could you show that code? – Mani Dec 28 '13 at 05:14
  • i am setting the delegate in my first ViewController. here is the sample of it. in .h file i am doing like this. `#import "ATAFoodParser.h" @interface NHMainViewController : UIViewController` and in .m `- (void)viewDidLoad { [super viewDidLoad]; ATAFoodParser *foodParser = [[ATAFoodParser alloc] init]; foodParser.delegate = self; }` – Saif Dec 28 '13 at 05:17
  • Too few informations. Pls check if the delegate object has dealloc. – LeverkusenFan Dec 28 '13 at 05:53
  • 1
    In your parser class, is there anywhere else that you use "delegate" ? There's nothing wrong with the code you've posted, so the problem is elsewhere. – rdelmar Dec 28 '13 at 18:04

2 Answers2

4

your problem appears to be that you are not keeping your "foodParser" around as a property or instance variable:

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

"foodParser" is a local variable, and it disappears (or is released by ARC) as soon as "viewDidLoad" finishes up.

To fix this, if you set a property in your NHMainViewController's .h file, e.g.

@property (strong) ATAFoodParser * foodparser;

You can then change that line in "viewDidLoad:" to:

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

and you should be good to go.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • thank michael. i tried this, still i am facing the same problem. i changed my local variable foodParser to a property.it is defined in .h file.. as u mentioned. – Saif Dec 28 '13 at 06:39
  • In the code edit I see above, you still have "`ATAFoodParser *foodParser =`" in your "`viewDidLoad`" method. That's a local variable. – Michael Dautermann Dec 28 '13 at 06:41
  • sorry forgot to change it here. i will do it now. thanks – Saif Dec 28 '13 at 06:45
  • and if you set an Xcode breakpoint in "`parserDidEndDocument`", delegate is *really* still nil? – Michael Dautermann Dec 28 '13 at 06:56
  • 2
    the only other thing I can imagine is happening is that you're doing the parsing while some other view controller (i.e. ***not*** `NHMainViewController`) is on screen and your "`NHMainViewController`" object has been fully dealloc'd & released. – Michael Dautermann Dec 28 '13 at 07:02
  • NHMainViewController is the only viewcontroller in my app. And i am parsing in seperate class called ATAFoodParser. After the parsing will get over i want the call back in my NHMainViewController. – Saif Dec 28 '13 at 07:11
0

I think, you have little bit confusion.

Just I give example of delegate as below.

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

and your delegate method implementation should be in .m file.

2) then where you have creating secondviewController, you have to do like below

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

From first view controller, you may push or present second view controller. Now you are in second. If you want to communicate with first, call method from secondView controller to firstView controller via delegate as you did.

Notes: Look at this beer bottle example , stack reference.

Community
  • 1
  • 1
Mani
  • 17,549
  • 13
  • 79
  • 100