委托方法没被调用?
我有一个视图控制器与一个应该被调用的委托方法,但它没有?
NotifyingViewController.h
@protocol NotifyingViewControllerDelegate @required - (void)iWasAccepted; @end @interface NotifyingViewController : UIViewController @property (nonatomic, weak) id delegate;
NotifyingViewController.m
-(void)someMethod{ [self.delegate iWasAccepted]; [self dismissViewControllerAnimated:YES completion:nil]; }
NotifiedViewController.h
#import "NotifyingViewController.h" @interface NotifiedViewController : UIViewController
NotifiedViewController.m
-(void)iWasAccepted{ [self saveIntoDB]; NSLog(@"DELEGATE RAN"); }
出于某种原因,应该通知的控制器不是。 通知控制器确实忽略了警告委托IS运行的方法,但委托不运行该函数,因为它不是NSLog。 有什么想法吗?
您不能只指定对象符合协议。 您还必须将该对象指定为委托。 当你分配/初始化NotifyingViewController的实例时,将它的委托设置为self,你应该没问题。
NotifyingViewController *notifyingInstance = [[NotifyingViewController alloc] init]; [notifyingInstance setDelegate:self];
这两个都很重要,并指定该类符合您已使用此行执行的协议。
@interface NotifiedViewController : UIViewController
另外,在调用委托方法时,最好将函数调用包装在respondsToSelector:
checks中。
if ([self.delegate respondsToSelector:@selector(iWasAccepted)]) { [self.delegate iWasAccepted]; }
- 来自iOS的Android AES解密和数据:javax.crypto.BadPaddingException:pad块损坏
- UIPageViewController:子控制器在滚动期间忽略状态栏高度
- 添加Xcode子项目:不应该复制所有源文件吗?
- 如何在iOS中正确地线性化OpenGL ES的深度?
- 将parameter passing给Swift中的Selector
- 在tableHeaderView中的UISearchController searchBar在屏幕上animation化
- 将“正确的”数据从UITableViewController中的选定单元传递给ViewController
- 在Mac上设置Appium 1.6.4(测试版)并在物理设备上测试您的App
- 使用两个字典键基于日期和时间对数组进行排序
- 只有当布尔条件满足,当restkit尝试更新logging时,才更新或保存被pipe理对象
- 如何在所有视图的顶部添加UIView?