我有一个必须实现多个协议的视图控制器类。太保持整洁我习惯将每个协议的方法放在视图控制器类的一个类别中。
这次我从链接器收到警告说该类没有实现协议之一。这些方法在运行时确实有效,链接器似乎无法识别类别中的实现。
我在一个不同的项目中简化了这个类,我在同一个地方得到了同样的错误。
类头:
#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
@interface TopVC : UIViewController
<
UINavigationControllerDelegate,
ABPeoplePickerNavigationControllerDelegate
>
{}
@end
TopVC.m(未显示)是自动生成的,没有任何更改。UINavigationControllerDelegate
协议方法在这个类别中实现:
#import <Foundation/Foundation.h>
#import "TopVC.h"
@interface TopVC (UINavigationControllerDelegate)
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
@end
#import "TopVC+UINavigationControllerDelegate.h"
@implementation TopVC (UINavigationControllerDelegate)
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
NSLog(@"navigationController:willShowViewController:animated:");
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
NSLog(@"navigationController:didShowViewController:animated:");
}
@end
链接器不会抱怨此类别中的此方法。但是,如果我尝试一个类别以ABPeoplePickerNavigationControllerDelegate
相同的方式实现协议,它会抱怨:
#import "TopVC.h"
@interface TopVC (ABPeoplePickerNavigationControllerDelegate)
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker;
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person;
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier;
@end
#import "TopVC+ABPeoplePickerNavigationControllerDelegate.h"
@implementation TopVC (ABPeoplePickerNavigationControllerDelegate)
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
return YES;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
return YES;
}
@end
链接器抱怨:
warning: incomplete implementation of class 'TopVC'
warning: method definition for '-peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier:' not found
warning: method definition for '-peoplePickerNavigationController:shouldContinueAfterSelectingPerson:' not found
warning: method definition for '-peoplePickerNavigationControllerDidCancel:' not found
warning: class 'TopVC' does not fully implement the 'ABPeoplePickerNavigationControllerDelegate' protocol
我能看到的唯一区别是UINavigationControllerDelegate
协议方法都是可选的,而ABPeoplePickerNavigationControllerDelegate
都是必需的。
然而,即使链接器抱怨,这些方法仍然在运行时被调用。我只是拒绝构建带有警告的构建。我显然错过了一些东西或在某处犯了一个微不足道的错误,但我无法发现它。