我的UITableView中有一个自定义单元格。此自定义单元格具有标签和文本框。当用户填写数据(4-5个字段)并单击保存按钮时。我想保存他输入的数据。
我该怎么做呢??
我只有大约5-6字段最多。如果你能给出一些例子,告诉我如何做到这一点,那就太好了。
发布于 2013-03-07 05:40:32
一种方法是:将数据保存在字典中。
发布于 2013-03-07 05:43:04
单击保存按钮后,您可以创建他将填充的数据的数据结构。将这些值设置为数据结构也可以使用NSMutableDictionary键值存储
例如:假设我们有4个UITextFeilds textFeild1,textFeild2,textFeild3,textFeild14
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic addObject: textFeild1.text forValue: @"val1"];
[dic addObject: textFeild2.text forValue: @"val2"];
[dic addObject: textFeild3.text forValue: @"val3"];
[dic addObject: textFeild4.text forValue: @"val4"];
//Now you have the values and can retrieve them by:
NSString *value1 = [dic valueForKey:@"val1"];发布于 2013-03-07 05:44:58
根据您的评论进行编辑:
在这种情况下,苹果建议使用Delegate-Pattern。基本上,你要做的是:
@protocol FirstDataVCDelegate;
@interface FirstDataVC : UIViewController
@property (weak, nonatomic) id<FirstDataVCDelegate> delegate;
//...
@end
@protocol FirstDataVCDelegate <NSObject>
- (void)firstDataVC:(FirstDataVC *)dataVC didCollectData:(NSDictionary *)data;
@end
@interface RootVC: UIViewController<FirstDataVCDelegate>
//...
@end
@implementation RootVC
- (void)firstDataVC:(FirstDataVC *)dataVC didCollectData:(NSDictionary *)data
{
NSString *name = [data valueForKey:@"name"];
[self.completeData setValue:name forKey:@"name"];
}
@end在这种情况下,RootVC将是想要发送整个数据的VC。FirstDataVC是用户输入数据的VC之一。
每个获得用户输入的VC都必须提供一个协议,该协议由RootVC实现。
Here更多的是关于委托。
https://stackoverflow.com/questions/15258412
复制相似问题