从Objective c中的其他viewcontrollers收集数据

时间:2018-02-25 09:13:56

标签: ios objective-c

我有7个ViewControllers,第一个是导航到五个视图控制器,最后一个是收集用户使用文本字段输入的数据,并用本地html表单替换它们。我只能从一个VC获得数据。所以我的问题是如何从所有VC中获取所有数据。这是第二个VC

- (IBAction)btnOpenHTML:(id)sender {
SecondVC *obj = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondVC"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

[dict setValue:self.txtSchool1Dgree.text forKey:@"degree1"];
[dict setValue:self.txtSchool2Dgree.text forKey:@"degree2"];

obj.dicData = dict;
[self.navigationController pushViewController:obj animated:true];}

这里是3ed VC:

- (IBAction)btnOpenHTML:(id)sender {

SecondVC *obj = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondVC"];

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

[dict setValue:self.txtComp1Name.text forKey:@"comp1name"];
[dict setValue:self.txtComp2Name.text forKey:@"comp2name"];

[self.navigationController pushViewController:obj animated:true];}

其他3VC相同。

对于接收器VC

-(NSString *)getHTMLStirng {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"resumeT" ofType:@"html"];
NSMutableString *strHTML = [NSMutableString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

if (self.dicData) {
    stringByReplacingOccurrencesOfString:@"#DEGREE1" withString:[_dicData valueForKey:@"degree1"]];
    stringByReplacingOccurrencesOfString:@"#DEGREE2" withString:[_dicData valueForKey:@"degree2"]];
    stringByReplacingOccurrencesOfString:@"#COMPANY1" withString:[_dicData valueForKey:@"comp1name"]];
    stringByReplacingOccurrencesOfString:@"#COMPANY" withString:[_dicData valueForKey:@"comp2name"]];
    }return strHTML;}

提前谢谢。

2 个答案:

答案 0 :(得分:0)

你可以在第三个VC做下一个:

obj.dicData = [self.dicData dictionaryByAddEntriesFromDictionary:dict];

你不需要NSMutableDictionary用于dict:

NSDictionary *dict = @{
    @"comp1name" : self.txtComp1Name.text,
    @"comp2name" : self.txtComp2Name.text
}

答案 1 :(得分:0)

或许为所有这些控制器制作一些通用模型会更好吗?所以他们每个人都可以使用这个模型的特定部分。它可能是一个新类或只是NSMutableDictionary对象 - 它取决于你。

控制器可以通过AppDelegate或特殊管理员(单身人士)访问模型。