Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将NSArray添加到UIButton(属性)_Ios_Objective C_Uibutton_Unrecognized Selector - Fatal编程技术网

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

Ios 如何将NSArray添加到UIButton(属性),ios,objective-c,uibutton,unrecognized-selector,Ios,Objective C,Uibutton,Unrecognized Selector,我正在为iPad开发xcode5,我需要一个带有自定义属性的自定义按钮类型,所以我添加了一个自定义类,如so 旋转按钮 #import <Foundation/Foundation.h> @interface UIButton(Property) @property (nonatomic, retain) NSString *typeOfContent; @property (nonatomic, retain) NSString *pdfDocumentName; @end

我正在为iPad开发xcode5,我需要一个带有自定义属性的自定义按钮类型,所以我添加了一个自定义类,如so

旋转按钮

#import <Foundation/Foundation.h>

@interface UIButton(Property)

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

@end
#import <Foundation/Foundation.h>

@interface UIButton(Property)

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

@end
@interface CarouselButton: UIButton

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

@end

这很好,现在我可以存储任何信息字符串类型 但是我想存储一个NSArray类型,所以我这样做了:

旋转按钮

#import <Foundation/Foundation.h>

@interface UIButton(Property)

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

@end
#import <Foundation/Foundation.h>

@interface UIButton(Property)

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

@end
@interface CarouselButton: UIButton

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

@end
但我得到了这个错误:

2014-04-08 18:36:37.320[10222:60b]-[ui]按钮 setViewControllerParameters::发送到实例的选择器无法识别 0x16591420 2014-04-08 18:36:37.323重新选择[10222:60b]*终止 应用程序由于未捕获异常“NSInvalidArgumentException”,原因: '-[UIButton setViewControllerParameters:]:已发送无法识别的选择器 到实例0x16591420' *第一次抛出调用堆栈:(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)类型的未捕获异常

在上述setter方法中,将“v”更改为大写字母“v”

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

所有这些关于关联对象的扭曲实际上只适用于类别,而不适用于子类。在实现中不需要这些代码;默认的setter和getter是由
@属性(非原子,retain)自动为您创建的

旋转按钮

#import <Foundation/Foundation.h>

@interface UIButton(Property)

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

@end
#import <Foundation/Foundation.h>

@interface UIButton(Property)

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

@end
@interface CarouselButton: UIButton

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

@end
旋转按钮

#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"
#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"

@implementation CarouselButton

@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"]];
#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"]];

您需要
设置ViewControllerParameters
。您的实现是
setviewControllerParameters
,请注意小写的
v
。我的坏消息是它现在可以正常工作了,谢谢!!!就这样!!!现在谢谢你works@Jesus,没问题。幸好这是一个容易解决的问题!