我有兩個控制器。一個BaseViewController和MyController。在BaseViewController我有物業,有一個協議iOS協議 - 委託始終爲零
BaseViewController.h
@property (nonatomic, strong) MyListener *myListener;
MyListener.h
@protocol MyListenerProtocol;
@interface MyListener : NSObject
@property (nonatomic, weak) id<MyListenerProtocol> delegate;
@end
@protocol MyListenerProtocol;
@protocol MyListenerProtocol <NSObject>
@optional
-(void) myMethod: (int) input;
@end
延伸myController的BaseViewController
@interface MyController : BaseViewController <MyListener>
,並在它的viewDidLoad一個NSObject :
super.myListener = [[MyListener alloc] init];
super.myListener.delegate = self;
在BaseViewController的方法,調用時它的作用:
if (self.myListener && [self.myListener.delegate respondsToSelector:@selector(myMethod:)]) {
[self.myListener.delegate myMethod: input];
}
但在這一點上, 「self.myListener」 始終是零。
我想要的是在調用BaseViewController的方法時調用子視圖控制器中的方法(但僅適用於某些子視圖控制器,而不是全部)。
有什麼建議嗎?
你確定它是相同的'MyListener'實例嗎? – rmaddy
爲什麼「超級」而不是「自我」? – vikingosegundo
@vikingosegundo我用兩種測試,「超級」是我最後的嘗試。沒有運氣。 – Panos