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 有关

4

1 回答 1

0

发现了问题。我的编程错误。我正在为错误的视图设置委托属性,所以问题出在调用类中。它现在工作正常。TomSwift 为我指明了正确的方向。谢谢。

于 2013-08-07T00:08:09.393 回答