0
votes

There are 3 classes Red, Blue and Yellow. Whenever a button is pressed yellow class informs the blue class and passes back a string through delegate. This bit works fine. Well now the blue class is trying to call the red class instance method displayString inside the delegate implementation method. But this instance method is never called for some reason. Upon debugging i found that self.red is null in [self.red displayString:col];. But i use self.red in another class instance method of Blue class and self.red is NOT NULL then. Is something missing here

@interface RedClass : NSObject 
     -(void)displayString:(NSString*)str;
@end

@implementation RedClass
     -(void)displayString:(NSString*)str
     {
         NSLog(@"The string:%@",str);
     }
@end

// BlueClass.h

    @interface BlueClass:UIViewController<YellowDelegate>
         @property (nonatomic, strong) RedClass *red;
    @end

//the class that is implementing the delegateExample and calling the class instance method of example class

// BlueClass.m

   @implementation BlueClass

   - (void)colorChanged:(NSString*)col
   {
      [self.red displayString:col];
   }
   @end

//the YellowClass

  @protocol YellowDelegate<NSObject> 
     -(void)colorChanged:(NSString *)col;
  @end
  @interface YellowClass:UIView
      @property (nonatomic, weak) id <YellowDelegate> delegate;
  @end

//YellowClass.m @implementation YellowClass

  (IBAction)btnPressed:(id)btn
   {
        [self.delegate colorChanged:@"yellow"];
   }
  @end
1
Where and how do you create an instance of RedClass in BlueClass? - rdelmar
How is this different from your question of mere hours ago? It seems like an abstraction of it stackoverflow.com/questions/18170130/… - Carl Veazey
yes it is the same.. i was wondering if there were any restrictions on calling an class instance methods from the delegation implementation. yes i have created the instance of RedClass in viewDidload(). i just skipped that.. that is the whole explanation of self.red being not null in other class instance method and it somehow is null in delegation implementation. So why does self.red become null in delegation implementation method when it works perfectly fine in other instance method - fftoolbar
It's hard to answer this without knowing when and where you're calling these other class instance methods that work correctly. And why this: "@property (nonatomic, strong) example *red", and not: "@property (nonatomic, strong) RedClass *red"? What is "example"? - rdelmar
What in the heck is a "class instance method"? There are class methods and there are instance methods. But class instance methods? Never heard of 'em. - bbum

1 Answers

0
votes

Show how you create and connect all the objects. Where do you allocate an instance of RedClass and assign it to the red @property? Where do you set the delegate?

If the answer is that you don't, then your code does nothing because, in Objective-C, nil eats messages.