ios - 委托(delegate)方法在使用 If 语句测试时调用失败,但在没有 If 语句的情况下正确调用

标签 ios objective-c cocoa-touch delegates uikit

我有这个 if 语句来检查我的委托(delegate)是否已经实现了给定的方法:

    if ([[self delegate] respondsToSelector:@selector(didFinishSettingNotificationOnDialog:)])
{
        [self.delegate didFinishSettingNotificationOnDialog:self withNotification:notification];

}

但是我的代码没有在 If 语句中执行。如果我删除 if 语句并只调用

,我还有其他委托(delegate)调用在这些对象 之间工作
[self.delegate didFinishSettingNotificationOnDialog:self withNotification:notification];

它本身就有效!

我的委托(delegate)确实正确实现了:

- (void)didFinishSettingNotificationOnDialog:(NotificationDialogView *)dialogView withNotification:(NotificationContext *)setNotification{
    NSLog(@"Notification is: %@", setNotification);
}

那我做错了什么?

最佳答案

方法名错误。它应该是 didFinishSettingNotificationOnDialog:withNotification:

试试这个:

 if ([[self delegate] respondsToSelector:@selector(didFinishSettingNotificationOnDialog:withNotification:)]) {
    [self.delegate didFinishSettingNotificationOnDialog:self withNotification:notification];
}

关于ios - 委托(delegate)方法在使用 If 语句测试时调用失败,但在没有 If 语句的情况下正确调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18155549/

相关文章:

ios - 第二个动画 block 中不遵守动画选项/错误

iphone - 释放 NSData 导致异常

ios - 记录中特定字段的 cloudkit 通知

ios - 使用 Xcode7 和 Swift 2 在 WebView 中查看 Parse PFFile PDF

ios - UITextField RightView 大小不正确

iphone - 如何设置组表的节标题文本

iphone - 如何在Autolayout视觉格式语言中使用NSNumber或CGFloat常量?

ios - UINavigationBar:添加无法更改颜色或标题的导航栏

iphone - iOS:使用父类(super class)的 getter 和 setter 的属性不再适用于 Xcode 4.4

javascript - 即使我需要设置我的 webView contentEditable=true,我怎样才能阻止键盘出现?