代表没有回复 - 如何调试

时间:2013-11-20 17:34:25

标签: ios cocoa-touch delegates childviewcontroller

我在视图控制器上创建了一个委托,这是我的代码:

FirstController.h

@protocol FirstControllerDelegate;

@interface FirstController : UIViewController
@property (nonatomic, strong) id< FirstControllerDelegate > delegate;
@end

@protocol FirstControllerDelegate <NSObject>
- (void) delegateMethod:(Testo *)testo;
@end

FirstController.m

@synthesize delegate; 

if ([self.delegate respondsToSelector:@selector(delegateMethod:)]) {
  NSLog(@"respond ok");
  [self.delegate delegateMethod:item];
}

SecondController.h

@interface SecondController : UIViewController < FirstControllerDelegate >

SecondController.m

self.firstController = [[FirstController alloc] initWithNibName:@"FirstController" bundle:nil];
[self.firstController setDelegate:self];

- (void) delegateMethod:(Testo *)testo
{
    NSLog(@"%@",testo);
}

问题是委托不响应选择器。

FirstController被添加为rootViewController的{​​{1}},UINavigationController被添加为childViewController SecondController

我以其他方式使用这种方式代理,我没有任何问题!

1 个答案:

答案 0 :(得分:0)

我唯一的猜测是,您在调用delegate时实际上未设置[self.delegate respondsToSelector:@selector(delegateMethod:)],导致nil调用返回FALSE