如何将NSArray添加到UIButton(属性)

时间:2014-04-08 23:58:50

标签: ios objective-c uibutton unrecognized-selector

我正致力于iPad的xcode5开发,我需要一个带有自定义属性的自定义按钮类型,所以我添加了一个自定义类,如此......

CarouselButton.h

#import <Foundation/Foundation.h>

@interface UIButton(Property)

@property (nonatomic, retain) NSString *typeOfContent;
@property (nonatomic, retain) NSString *pdfDocumentName;

@end

CarouselButton.m

#import "CarouselButton.h"
#import <objc/runtime.h>

@implementation UIButton(Property)

static char UIB_PROPERTY_KEY_1;
static char UIB_PROPERTY_KEY_2;

@dynamic typeOfContent;
@dynamic pdfDocumentName;

-(void)setTypeOfContent:(NSObject *)typeOfContent{
    objc_setAssociatedObject(self, &UIB_PROPERTY_KEY_1, typeOfContent, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSObject*)typeOfContent{
    return (NSObject*)objc_getAssociatedObject(self, &UIB_PROPERTY_KEY_1);
}

-(void)setPdfDocumentName:(NSObject *)pdfDocumentName{
    objc_setAssociatedObject(self, &UIB_PROPERTY_KEY_2, pdfDocumentName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSObject*)pdfDocumentName{
    return (NSObject*)objc_getAssociatedObject(self, &UIB_PROPERTY_KEY_2);
}

@end

以及填充和调用这些属性进行操作的方法:

#import "CarouselButton.h"


UIButton *button = (UIButton *)view;
button          = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.typeOfContent    = [[itemsCarouselTop objectAtIndex:index] objectForKey:@"typeOfContent"];
button.pdfDocumentName  = [[itemsCarouselTop objectAtIndex:index] objectForKey:@"pdfDocumentName"];

这个工作正常,现在我可以存储任何信息NSString类型 但是我想存储一个NSArray类型,所以我这样做了:

CarouselButton.h

#import <Foundation/Foundation.h>

@interface UIButton(Property)

@property (nonatomic, retain) NSString *typeOfContent;
@property (nonatomic, retain) NSString *pdfDocumentName;
@property (nonatomic, retain) NSArray *viewControllerParameters;

@end

CarouselButton.m

#import "CarouselButton.h"
#import <objc/runtime.h>

@implementation UIButton(Property)

static char UIB_PROPERTY_KEY_1;
static char UIB_PROPERTY_KEY_2;
static char UIB_PROPERTY_KEY_3;

@dynamic typeOfContent;
@dynamic pdfDocumentName;
@dynamic viewControllerParameters;

-(void)setTypeOfContent:(NSObject *)typeOfContent{
    objc_setAssociatedObject(self, &UIB_PROPERTY_KEY_1, typeOfContent, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSObject*)typeOfContent{
    return (NSObject*)objc_getAssociatedObject(self, &UIB_PROPERTY_KEY_1);
}

-(void)setPdfDocumentName:(NSObject *)pdfDocumentName{
    objc_setAssociatedObject(self, &UIB_PROPERTY_KEY_2, pdfDocumentName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSObject*)pdfDocumentName{
    return (NSObject*)objc_getAssociatedObject(self, &UIB_PROPERTY_KEY_2);
}

-(void)setviewControllerParameters:(NSObject *)viewControllerParameters{
    objc_setAssociatedObject(self, &UIB_PROPERTY_KEY_3, viewControllerParameters, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSObject*)viewControllerParameters{
    return (NSObject*)objc_getAssociatedObject(self, &UIB_PROPERTY_KEY_3);
}

@end

我用这种方式:

#import "CarouselButton.h"


UIButton *button = (UIButton *)view;
button          = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.typeOfContent    = [[itemsCarouselTop objectAtIndex:index] objectForKey:@"typeOfContent"];
button.pdfDocumentName  = [[itemsCarouselTop objectAtIndex:index] objectForKey:@"pdfDocumentName"];
button.viewControllerParameters = [NSArray arrayWithArray:[[itemsCarouselTop objectAtIndex:index] objectForKey:@"viewControllerParameters"]];

但是我收到了这个错误:

  

2014-04-08 18:36:37.320 [10222:60b] - [UIButton   setViewControllerParameters:]:发送到实例的无法识别的选择器   0x16591420 2014-04-08 18:36:37.323 eSelect [10222:60b] *终止   应用程序由于未捕获的异常&#39; NSInvalidArgumentException&#39;,原因:   &#39; - [UIButton setViewControllerParameters:]:发送无法识别的选择器   例如0x16591420&#39;   * 第一次抛出调用堆栈:(0x308f8f03 0x3b4e2ce7 0x308fc837 0x308fb12f 0x3084a0d8 0xeaa7d 0x110b0f 0x1111d9 0x10f6d3 0x1103c1   0x10a43d 0xe97af 0x3312ca53 0x331d730d 0x331d7223 0x331d6801   0x331d6529 0x331d6299 0x331d6231 0x33128305 0x32da431b 0x32d9fb3f   0x32dceb4d 0x331a489f 0x331a2ea9 0x331a20e7 0x331a206f 0x331a2007   0x3319a681 0x3312e697 0x331a1d59 0x331a1829 0x33133615 0x3319982b   0x12220b 0x11a09d 0x332467f3 0x332f8cb3 0x331a7e09 0x33120b57   0x308c4031 0x308c19bf 0x308c1d0b 0x3082c7a9 0x3082c58b 0x357736d3   0x3318b891 0x120361 0x3b9e0ab7)libc ++ abi.dylib:终止于   NSException(lldb)

类型的未捕获异常

2 个答案:

答案 0 :(得分:3)

-(void)setviewControllerParameters:(NSObject *)viewControllerParameters{
    objc_setAssociatedObject(self, &UIB_PROPERTY_KEY_3, viewControllerParameters, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

在上面的setter方法中,更改&#39; v&#39;到首都&#39; V&#39;。

E.g。 setViewControllerParameters:不是setviewControllerParameters:

答案 1 :(得分:1)

除非您希望应用程序中的每个按钮都具有这些属性,否则您应该将UIButton子类化为CarouselButton类,而不是在UIButton上创建“属性”类别。

所有关于AssociatedObjects的扭曲实际上只适用于类别,而不是子类所必需的。您的实现中不需要任何代码; @property (nonatomic, retain)

会自动为您创建默认设置器和getter

CarouselButton.h

@interface CarouselButton: UIButton

@property (nonatomic, retain) NSString *typeOfContent;
@property (nonatomic, retain) NSString *pdfDocumentName;
@property (nonatomic, retain) NSArray *viewControllerParameters;

@end

CarouselButton.m

#import "CarouselButton.h"

@implementation CarouselButton

@end

并以这种方式使用它:

#import "CarouselButton.h"

CarouselButtton *button = [CarouselButtton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.typeOfContent    = [[itemsCarouselTop objectAtIndex:index] objectForKey:@"typeOfContent"];
button.pdfDocumentName  = [[itemsCarouselTop objectAtIndex:index] objectForKey:@"pdfDocumentName"];
button.viewControllerParameters = [NSArray arrayWithArray:[[itemsCarouselTop objectAtIndex:index] objectForKey:@"viewControllerParameters"]];