在我的应用程序中,我使用了3个类MasterViewController,DeviceViewController和BRButton。 BRButton类完成所有的实际工作,并为其蓝牙4.0设备提供硬件委托回调。我试图在BRButton中发生某些事情时让方法在其他类中触发。这是在DeviceViewController工作,但不是在MasterViewController中,我仍然非常绿色与IOS编程任何帮助表示赞赏。这里是一些代码片段。
我应该提到的还有一件事是它没有错误,它只是不调用方法。使用协议的跨班通信
BRButton.h
@protocol RefreshScreen
-(void) tableTimer:(NSTimer *)timer;
@end
@protocol BRButtonDelegate
@optional
-(void) bounceBack;
-(void) tableTimer:(NSTimer *)timer;
@required
-(void) buttonReady;
@end
@interface BRButton : NSObject <CBCentralManagerDelegate, CBPeripheralDelegate> {
NSTimer *myTimer;
}
@property (nonatomic,assign) id <BRButtonDelegate> delegate;
@property (nonatomic,assign) id <RefreshScreen> testCall;
BRButton.m(这里是2米不同的地方,我调用不同类的东西。第一个不工作的第二个呢。)
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
...does some work then...
[[self testCall] tableTimer:nil]; //tableTimer is nil here because no NStimer is used to trigger from this delegate method.
printf("New UUID, adding\r\n");
printf("didDiscoverPeripheral\r\n");
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
printf("Disconnecting \r\n");
[[self delegate] bounceBack];
}
MasterViewController .h
@interface MasterViewController : UIViewController < UITableViewDelegate, UITableViewDataSource, RefreshScreen> {
BRButton *b;
}
MasterViewController.m(我希望这会在我的断点后调用o ñdidDiscoverPeripheral,但它永远不会是。)
-(void) tableTimer:(NSTimer *)timer {
if(b.peripherals.count > 0)
{
[self.deviceTableView reloadData];
}
}
DeviceViewController.h
@interface DeviceViewController : UIViewController <BRButtonDelegate> {
}
@property (strong, nonatomic) BRButton *bb;
DeviceViewController.m
- (void) bounceBack {
[self.navigationController popViewControllerAnimated:YES];
}
如果你不再需要的信息让我知道。
你可以显示你在哪里设置委托和“testCall”?使用断点并确保它在调用tableTimer时不为零。 – 2012-01-03 23:28:29
就是这样。我在需要时设置委托,但从未设置testCall。把一个简单的b.testCall = self;现在它的所有工作。非常感谢。 – Merlin910 2012-01-03 23:52:29
不适合作为回答,所以你可以接受,如果你想要:P – 2012-01-03 23:56:02