1
我創建了一個包含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
你甚至試圖使用關聯對象hackage來實現什麼? – 2013-08-06 17:02:07
我創建了一個行爲如下拉列表的視圖子類。現在我想在其他視圖上使用動畫行爲(比如UIButton),所以我決定創建一個可以在任何視圖上使用的類別。除了通知完成動畫之外,我已經做了所有工作。 – JVC
你不顯示任何代碼設置delegateForDropdown自我。但我想你是這麼做的? – TomSwift