0

初心者なので、デリゲートを理解するのが非常に難しいと感じているので、できる限り質問をしようと思います。

Connection.m を Controller.h のデリゲートにしたい。私の Controller.h には

@protocol HeliControllerDelegate <NSObject>

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

@end

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

Controller.m で合成します。

@synthesize delegate = _delegate;

インターフェイス宣言の前。Controller.m で didDiscoverCharacteristic を呼び出す

- (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];

    }
}
}

委任ファイル Connection.h で、私は

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

Controller.h のプロトコルのメソッドを使用できるようにしますが、プログラムが didDiscoverCharacteristic 呼び出しを実行しても、Connection.m のメソッド実装では何も起こりません。

これに関するすべてのヘルプは本当に感謝しています。

4

4 に答える 4

1

Controoler.hファイルでデリゲートを宣言しましたか?

@interfaceコントローラー
{{
    id(ControllerDelegate)controllerDelegate;
}
@property(nonatomic、assign)id(ControllerDelegate)controllerDelegate;

ControllerDelegateには()の代わりに<>括弧を使用し、以下を.mファイルに追加します

@synthesize controllerDelegate
于 2012-07-16T08:21:40.497 に答える
1

あなたはまだ何かを見逃しています:

「コントローラー」のインスタンスに、どのインスタンスがそのデリゲートであるかを伝える必要があります。つまり、プロパティを作成し (「デリゲート」または「myDelegate」と同様のものと呼びます)、使用するクラスのインスタンスを指すように設定する必要があります。そのプロトコル(あなたの場合は Connection )。

UITableのデリゲートと同様に機能するはずです(プロトコルUITableViewDelegateなど)

tableView:didSelectRowAtIndexPath: およびその他のデリゲートメソッドを呼び出すために、誰が(どのインスタンスが)「聞く」ことができるかを UITableView に伝える必要があります

それを指すテーブル インスタンス プロパティを設定します。

yourTable.delegate = anInstanceOfADelegateClass;

于 2012-07-16T08:25:19.407 に答える
0

Controller.mself.delegateでは、 を呼び出すと nil になる可能性がありますdidDiscoverCharacteristic。これは、デバッガーまたは NSLog() で確認できます。Controller オブジェクトのデリゲート@propertyを Connection の正しいインスタンスに設定していることを確認してください。

于 2012-07-16T08:24:40.807 に答える
0
  1. ControllerDelegate の後の NSObject には意味がありません

  2. Controoler クラスでデリゲート属性を宣言する

于 2012-07-16T08:24:57.933 に答える