Objective-C委托-在本例中无法查看委托的工作方式
我在iPhone上运行一个应用程序,有一行代码如下:Objective-C委托-在本例中无法查看委托的工作方式,objective-c,delegates,Objective C,Delegates,我在iPhone上运行一个应用程序,有一行代码如下: [self.searchControllerDelegate searchController:self didSelectSearchTerm:search.text]; .h文件定义委托和属性: @protocol NBMSearchViewControllerDelegate <NSObject> - (void)searchController:(NBMSearchViewController *)controller
[self.searchControllerDelegate searchController:self didSelectSearchTerm:search.text];
.h
文件定义委托和属性:
@protocol NBMSearchViewControllerDelegate <NSObject>
- (void)searchController:(NBMSearchViewController *)controller didSelectSearchTerm:(NSString *)term;
@end
@property (weak, nonatomic) IBOutlet id<NBMSearchViewControllerDelegate> searchControllerDelegate;
我在setupApplicationForiPhone
方法中没有看到对searchControllerDelegate
的引用(fwiw,在setupApplicationForiPad
方法中有一个)
困惑。有人能解释一下吗?这个项目可以
在AppDelegate
中,将对象设置为NBMSearchViewController的delegate
,因为有两个不同的UIViewControllers
初始化,这取决于启动应用程序的平台(iPhone/iPad)。在我看来,iPhone上不可能进行搜索
您还可以使用CMD选择委托方法,然后获得将在何处执行这些方法的提示:
不太可能,因为这是您的代码库,而且听起来像是基于文本搜索的问题(对
searchControllerDelegate
的全局搜索)。代理是否在iPhone上调用?代码库来自“使用UI自动化测试iOS应用程序”一书。不,我没有看到代表在iPhone上被调用。所以听起来你已经有了答案,大概你在iPad上看到了答案
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
[self setupApplicationForiPad];
} else {
[self setupApplicationForiPhone];
}