0

Being a beginner I am finding it very hard to understand delegates, so I'll try to ask my question as good as I can:

I want Connection.m to be the delegate of Controller.h. In my Controller.h I have

@protocol HeliControllerDelegate <NSObject>

@optional
- (void) measurementUpdated:(NSNumber *) measurement;
- (void) didDiscoverCharacteristic; // neh

@end

@interface HeliController : UIViewController <CBPeripheralDelegate>
@property (nonatomic, assign) id<HeliControllerDelegate> delegate;
@end

and synthesize in Controller.m :

@synthesize delegate = _delegate;

before the interface decleration. In Controller.m I invoke didDiscoverCharacteristic with

- (void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{

NSLog(@"Did discover characteristic for service %@", [service.peripheral UUID]);

for(CBCharacteristic *c in [service characteristics]){

    if([[c UUID] isEqual:HeliController.throttleCharacteristicUUID]){

        NSLog(@"Found throttle characteristic");

        self.throttleCharacteristic = c;

        [self.delegate didDiscoverCharacteristic];

    }
}
}

In the delegate file Connection.h I start with

@interface Connection : UIViewController <CBCentralManagerDelegate, ControllerDelegate> {
}

so that I can use the methods from Controller.h's protocol, but even thoug the program executed the didDiscoverCharacteristic call nothing happens on the method implementation in Connection.m.

All help on this is really appreciated.

chwi
  • 2,752
  • 2
  • 41
  • 66
  • 1
    Please provide a bit more code. Where was self.delegate set? In IB or somewhere in your code? To what type of object is self.delegate set? Did you actually implement didDiscoverWithCharacteristic? And How? Wher exactly do you call [self.delegate didDiscoverCharacteristic]? – Hermann Klecker Jul 16 '12 at 08:21
  • 1. Do you implement `didDiscoverCharacteristic` in Connection.m? 2. Do you assign an instance of `Connection` to Controller's delegate property? – Pavel Reznikov Jul 16 '12 at 08:22
  • I still don't get this. I need some code examples which could show me as easy as possible, derived from my code how to do this. – chwi Jul 16 '12 at 08:41
  • Have a look at this post: http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c – Hermann Klecker Jul 16 '12 at 08:50
  • I tried but didn't manage to understand it. Things get a little messed up since Controller is delegate for CBPeripheral, while Connection should be delegate for Controller. – chwi Jul 16 '12 at 08:54

4 Answers4

1

Did you declare delegate in Controoler.h file?

@interface Controller 
{
    id (ControllerDelegate) controllerDelegate;
}
@property (nonatomic, assign) id (ControllerDelegate) controllerDelegate;

use <> brackets instead of () for ControllerDelegate and add below in .m file

 @synthesize controllerDelegate 
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54
spaleja
  • 1,435
  • 15
  • 24
  • It didn't like both iVar and property with same name, so I remove the iVar and synthesize with `@synthesize controllerDelegate = _controllerDelegate`. But how do I set Connection to be the delegate now then? – chwi Jul 16 '12 at 08:38
  • if (self.profilePhotoDelegate != nil && [self.delegate respondsToSelector:@selector(didDiscoverCharacteristic)]) { [self.delegate didDiscoverCharacteristic]; } and in ur connection.h file declare instant for Controller Controller *controller; and in .m file controller.delegate = self; – spaleja Jul 16 '12 at 08:55
  • implement didDiscoverCharacteristic method in .m file - (void) didDiscoverCharacteristic { NSLog(@"didDiscoverCharacteristic"); } – spaleja Jul 16 '12 at 09:02
1

you still miss something:

you have to tell your instance of "Controller" which instance is its delegate, meaning you have to create a property (call it "delegate" or something similar as "myDelegate") and set it to point to an instance of a class that uses that protocol ( Connection in your case).

it should work as for delegates of UITable (with protocol UITableViewDelegate, e.g.)

you need to tell your UITableView who is (which instance) can "hear" to call to tableView:didSelectRowAtIndexPath: and other delegate methods

setting your table instance property pointing to it:

yourTable.delegate = anInstanceOfADelegateClass;

meronix
  • 6,175
  • 1
  • 23
  • 36
  • How do I point it to the instance? Code snippet would be great – chwi Jul 16 '12 at 08:36
  • 1
    Helicopter * helico = [[[Helicopter alloc]init] autorelease]; Helico.delegate= self; (Where self I assume where you implemented delegate method and initialized Helicopters object) – J S Rodrigues Jul 16 '12 at 08:58
0

In Controller.m self.delegate is likely nil when you call didDiscoverCharacteristic. You can check this with the debugger or NSLog(). Ensure you are setting the delegate @property of your Controller object to the correct instance of Connection.

torrey.lyons
  • 5,519
  • 1
  • 23
  • 30
0
  1. the NSObject after ControllerDelegate has no sense

  2. declare a delegate attribute in your Controoler class

touti
  • 1,164
  • 6
  • 18