Ios 未调用委托方法?

Ios 未调用委托方法?,ios,objective-c,cocoa-touch,uiviewcontroller,delegates,Ios,Objective C,Cocoa Touch,Uiviewcontroller,Delegates,我有一个视图控制器,它有一个应该调用的委托方法,但它没有 通知ViewController.h @protocol NotifyingViewControllerDelegate <NSObject> @required - (void)iWasAccepted; @end @interface NotifyingViewController : UIViewController @property (nonatomic, weak) id<NotifyingViewCon

我有一个视图控制器,它有一个应该调用的委托方法,但它没有

通知ViewController.h

@protocol NotifyingViewControllerDelegate <NSObject>
@required
- (void)iWasAccepted;
@end

@interface NotifyingViewController : UIViewController

@property (nonatomic, weak) id<NotifyingViewControllerDelegate> delegate;
#import "NotifyingViewController.h"  
@interface NotifiedViewController : UIViewController <NotifyingViewControllerDelegate>
通知dviewcontroller.h

@protocol NotifyingViewControllerDelegate <NSObject>
@required
- (void)iWasAccepted;
@end

@interface NotifyingViewController : UIViewController

@property (nonatomic, weak) id<NotifyingViewControllerDelegate> delegate;
#import "NotifyingViewController.h"  
@interface NotifiedViewController : UIViewController <NotifyingViewControllerDelegate>

由于某种原因,应该通知的控制器没有收到通知。通知控制器确实会关闭,这意味着向委托发出警报的方法已运行,但委托不会运行该函数,因为它不会记录。你知道为什么吗?

你不能只指定一个对象符合协议。还必须将该对象指定为委托。当您alloc/init NotifyingViewController的实例时,将其委托设置为self,您就可以了

NotifyingViewController *notifyingInstance = [[NotifyingViewController alloc] init];
[notifyingInstance setDelegate:self];
重要的是要做到这一点,并指定类符合协议,这一点您已经在使用这一行进行了

@interface NotifiedViewController : UIViewController <NotifyingViewControllerDelegate>

您将
NotifiedViewController
分配到哪里作为
NotifyingViewController
的委托?您的
委托
是一个
引用(应该是);您确定在调用它时它没有变为
nil
?@0x7fffffff:我以为您只需要在UIViewController:NSObject部分执行它?还有别的地方需要我放吗?抱歉,我对委托方法不熟悉:)您能解释一下为什么推荐最后提到的实践吗?@DariusMiliauskas这就是您在运行时检查委托是否实际实现了该方法的方式。即使您将一个方法标记为协议中所需的方法,您实际上也不会被迫实现它(如果不这样做,您只会收到一条警告)。如果您有一个未实现的委托方法(可选或必需),并且您试图调用该方法,则运行时将通过
NSInvalidArgumentException
,并抱怨“无法识别的选择器如何发送到实例”。简言之,此检查仅允许您在委托实例实际实现该方法时调用该方法。如果([self.delegate respondsToSelector:@selector(iWasAccepted)]{[self.delegate iWasAccepted];}如何解决该问题,我将实现该方法,但不会进入条件?
if ([self.delegate respondsToSelector:@selector(iWasAccepted)]) {
    [self.delegate iWasAccepted];
}