简体   繁体   English

在NSUserdefaults问题中存储custom_button?

[英]storing custom_button in NSUserdefaults issue?

i am storing my "custom button" in NSUserdefaults using the following code.But i am getting an error "[UIImage encodeWithCoder:]: unrecognized selector sent to instance" while converting the object to NSdata..here "custom button" is UIButton class. 我使用以下代码将“自定义按钮”存储在NSUserdefaults中。但是,在将对象转换为NSdata时收到了“ [UIImage encodeWithCoder:]:无法识别的选择器发送到实例”的错误。这里“自定义按钮”是UIButton类。 Anyone know why...? 有人知道为什么吗? please help me. 请帮我。

 Custom_button *lock11 = (Custom_button*)[menu1 viewWithTag:100];
                 NSLog(@"opened lock1 ========= %@",lock11);
                 lock11.is_menu_lock_opened = YES; 

                 NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:lock11]; //[NSKeyedArchiver archivedDataWithRootObject:lock11];
                 [prefs setObject:myEncodedObject forKey:@"set1lock"];

The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. NSUserDefaults类提供了用于访问常见类型(例如浮点数,双精度数,整数,布尔值和URL)的便捷方法。 A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. 默认对象必须是属性列表,即NSData,NSString,NSNumber,NSNumber,NSDate,NSArray或NSDictionary的一个实例(或用于集合的实例的组合)。 If you want to store any other type of object, you should typically archive it to create an instance of NSData. 如果要存储任何其他类型的对象,通常应将其归档以创建NSData实例。

Objects that don't map directly to property list objects are turned into NSData by being sent a coder and encoding their contents. 通过直接发送给编码器并对其内容进行编码,可以将未直接映射到属性列表对象的对象转换为NSData [UIImage encodeWithCoder:] . [UIImage encodeWithCoder:] They need to conform to the NSCoding protocol for this to work. 他们需要遵循NSCoding协议才能起作用。 You will find that UIImage does not confirm to the NSCoding protocol before iOS 5. If you want to deploy before iOS 5 you will have to work something out yourself, by implementing NSCoding in your Custom Button class and storing that image in a different way. 您会发现UIImage不会在iOS 5之前确认NSCoding协议。如果您想在iOS 5之前进行部署,则必须自己解决问题,方法是在“自定义按钮”类中实现NSCoding并以其他方式存储该图像。

Implement this method into your Custom_button Class 在您的Custom_button类中实现此方法

initWithCoder and encodeWithCoder for all object 所有对象的initWithCoder和encodeWithCoder

-(id) initWithCoder: (NSCoder *)coder {

       self = [[CastInnerListData alloc] init];
       if (self != nil) {       
           self.object1 = [coder decodeObjectForKey:@"object1"];
           self.object2 = [coder decodeObjectForKey:@"object2"];    
    }
    return self;
}

-(void) encodeWithCoder: (NSCoder *)coder{

      [coder encodeObject:object1 forKey:@"object1"];
      [coder encodeObject:object2 forKey:@"object2"];
}

For more detail click here 欲了解更多详情,请点击这里

Well a little searching would have given you the answer fast: 好一点的搜索就会很快给您答案:

UIImage encodeWithCoder urecognised selector? UIImage encodeWithCoder urecognised选择器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM