我有一个类和一个视图控制器。类有 _delegate respondsToSelector:selector。但它没有调用方法(选择器)。查看下面的代码后,请您告诉我我做错了什么。仅供参考,此代码以前有效,但突然停止工作。
MyClass.h:
@protocol MyClassDelegate <NSObject>
@optional
-(void) receivedEvent:(TripEvent *) event;
@end
@interface MyClass : NSObject<MyClassEngine>
@property(assign) id<MyClassDelegate> delegate;
@property BOOL delegateOnMainThread;
-(id) initWithDelegate:(id<UbiTripRecorderDelegate>) tripDelegate
onMainThread:(BOOL) onMainThread;
- (void) dispatchDelegate:(SEL)selector
withObject:(id)obj;
MyClass.m 包含以下代码:
#import "MyClass.h"
@implementation MyClass
....
....
-(void) startPlaying {
[self dispatchDelegate:@selector(receivedEvent:) withObject:newEvent];
}
- (void) dispatchDelegate:(SEL)selector withObject:(id)obj {
@try {
if(_delegate && [_delegate respondsToSelector:selector]) {
NSLog(@"I can't come to this point");
}
}
}
我还在视图控制器 (MyViewController.h) 中添加了以下代码:
@interface MyViewController : BaseViewController<MyClassDelegate>
最后在 MyViewcontroller.m 中实现了该方法:
-(void) receivedEvent : (TripEvent *) event {
NSLog(@"I'm called");
}
问题是 if(_delegate && [_delegate respondsToSelector:selector]) - 逻辑永远不会返回 true。任何人都可以检查代码并让我知道,如果我的代码有任何问题吗?