2013-08-06 28 views
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

+0

你甚至試圖使用關聯對象hackage來實現什麼? – 2013-08-06 17:02:07

+0

我創建了一個行爲如下拉列表的視圖子類。現在我想在其他視圖上使用動畫行爲(比如UIButton),所以我決定創建一個可以在任何視圖上使用的類別。除了通知完成動畫之外,我已經做了所有工作。 – JVC

+0

你不顯示任何代碼設置delegateForDropdown自我。但我想你是這麼做的? – TomSwift

回答

0

發現問題。編程錯誤,我的一部分。我設置了錯誤視圖的委託屬性,所以問題出現在調用類中。現在它工作正常。 TomSwift指出我正確的方向。謝謝。