我创建了一个具有 2 个属性的类别,但在尝试将一个属性用作代表时遇到了问题。
// UIView+Dropdown.h
#import <UIKit/UIKit.h>
@protocol DropDownAnimationDoneDelegate <NSObject>
-(void) onDropDownAnimationDone:(id)sender;
@end
@interface UIView (Dropdown)
@property (strong, nonatomic) id <DropDownAnimationDoneDelegate> delegateForDropDown;
@property (nonatomic,assign) BOOL isDropped;
// UIView+Dropdown.m
#import "UIView+Dropdown.h"
#import <objc/runtime.h>
@implementation UIView (Dropdown)
-(void)setDelegateForDropDown:(id)ddDelegate{
objc_setAssociatedObject(self, @selector(delegateForDropDown),ddDelegate,OBJC_ASSOCIATION_RETAIN_NONATOMIC);}
-(id)delegateForDropDown{
return objc_getAssociatedObject(self, @selector(delegateForDropDown));}
-(void)setIsDropped:(BOOL)dropIt{
objc_setAssociatedObject(self, @selector(isDropped), [NSNumber numberWithBool:dropIt], OBJC_ASSOCIATION_RETAIN_NONATOMIC);}
-(BOOL)isDropped{
return [objc_getAssociatedObject(self, @selector(isDropped)) boolValue];}
动画块完成后,委托将用于通知:
[UIView animateWithDuration:0.75
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{self.center = newCenter;}
completion:^(BOOL finished){
if ([[self delegateForDropDown] respondsToSelector:@selector(onDropDownAnimationDone:)])
[[self delegateForDropDown] onDropDownAnimationDone:self];}];
我的问题delegateForDropDown
总是包含nil
. 布尔属性工作正常,所以我怀疑它与委托的类型是 id 有关