0

C# code:

public delegate void ItemFound(ChunkDetails ObjChunkDetails);
public event ItemFound eventItemFound;

How to declare the above event delegates mechanism in Objective-C?

I tried to declare delegates method in objective-c but still i need help.

Objective-C:

+ (void) ItemFound:(ChunkDetails *)ObjItemDetails;

So how to declare signature of events and delegates in .h file of Objective-C?

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
user3200854
  • 147
  • 2
  • 8
  • 1
    You should probably read Apples [Programming with Objective-C - Working with Protocols](https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html). (Or even the whole document about Objective-C) – Matthias Bauch Jan 24 '14 at 11:41

3 Answers3

3

The iOS developer guide has a good section on delegation. But here's a brief example for you. In iOS, you usually declare a protocol for a delegate to implement:

@protocol BatteryMonitorDelegate <NSObject>
- (void)batteryMonitor:(BatteryMonitor *)monitor didUpdateBatteryLevel:(NSNumber *)batteryLevel;
@end

You would then declare a property on your class for your delegate. This property will be for an object of a type that implements the above protocol:

@interface BatteryMonitor : NSObject

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

@end

You'll then implement the protocol in a custom class, to handle any any methods it defines:

@interface SomeClass : NSObject <BatteryMonitorDelegate>
@end

@implementation SomeClass

- (void)batteryMonitor:(BatteryMonitor *)monitor didUpdateBatteryLevel:(NSNumber *)batteryLevel
{
    // handle the delegate callback
}

@end

Finally, you can call the delegate method when required, from your original object:

@implementation BatteryMonitor

- (void)someMethod
{
    // code

    if ([self.delegate respondsToSelector:@selector(batteryMonitor:didUpdateBatteryLevel)])
    {
        [self.delegate batteryMonitor:self didUpdateBatteryLevel:batteryLevel];
    }
}    

@end
James Frost
  • 6,960
  • 1
  • 33
  • 42
1

In the example where class A calls class B to perform an action and then return something we would do this:

Class B's .h, this goes above the @interface

@protocol CLASSBNAMEDelegate <NSObject>

- (void) YOURMETHOD:(id) returnValue

@end

Then under the @interface we add a delegate property:

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

In class b .m where you want to send a message back to Class A you would:

 if ([self.delegate respondsToSelector:@selector(YOURMETHOD:)]) {
    [self.delegate YOURMETHOD:value];
}

In Class A, where you use Class B be sure to set the delegate like so:

ClassB *b = [Class B etc....];
[b setDelegate:self];

IN Class A Header make sure you:

@interface CLASSA : NSObject <CLASSBNAMEDelegate>

Then you would need to respond to the selector:

- (void) YOURMETHOD:(id) value{}

Hope this helps...

CW0007007
  • 5,681
  • 4
  • 26
  • 31
-1
suppose there are two class named as Sender and Receiver. when class sender makes some event and you want some method to be executed in Receiver. then this is the process:

 step 1: declaration of protocol

in Class Sender,
@protocol senderDelegate <NSObject>

- (void) eventProcessed;

@end

 step 2: make a property delegate
@property (nonatomic, weak) id <senderDelegate> delegate;
    it should be nonatomic and only.
 step 3: in class sender only where u want to execute this delegate method, do following things-
   if ([self.delegate respondsToSelector:@selector(eventProcessed)]) {
    [self.delegate eventProcessed];
}

 step 4: now jump in Receiver class, and add delegate like
@interface Receiver : UIViewController <senderDelegate>
  and don't forget to import sender class

 Step 5: delegate should be set before executing sender class
   Sender *obj = [[Sender alloc]init];
   [obj setdelegate:self];

 step 6: implement your methods
 - (void) eventProcessed
  {
     NSLog(@"enjoying with delegate");
  }

  i think it is simple and understandable, please let me know if  u need any more clarification. Thanks..
manish
  • 109
  • 1
  • 11