如何将数据从父视图控制器传递到子视图控制器? [英] How can I passing data from parent view controller to child view controller?

查看:175
本文介绍了如何将数据从父视图控制器传递到子视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将数据从父视图控制器传递给子视图控制器?



我尝试这样做与代理。但我不认为这是一个选择?

解决方案

选项1



如果您从父级传递到子视图控制器,那么只需从父级设置childViewController的属性。

  customChildViewController.someRandomProperty = @一些随机值; 
[self presentViewController:customChildViewController animated:YES completion:nil];



选项2



或者,你可以设置委托



步骤1:在ChildViewController.h文件中设置界面上方

 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $;;;;;;;;;;;;;;;;;; 

@end

@interface .....

步骤2:在ChildViewController.h界面中创建委托属性

  @property(strong,nonatomic)id< ChildViewControllerDelegate>委托

步骤3:在ParentViewController.h中声明委托协议

  @interface ParentViewController:UIViewController< ChildViewControllerDelegate> 

步骤4:在ParentViewController.m中添加此方法:

   - (NSDictionary *)giveMeData {
NSMutableDictionary * dataToReturn = [[NSMutableDictionary alloc] init];
dataToReturn [@some] = @random;
dataToReturn [@data] = @here;
return dataToReturn;
}

步骤5:在启动childViewController之前声明委托属性。

  childViewController.delegate = self; 
[self presentViewController:childViewController animated:YES completion:nil];

步骤6:在子视图控制器中,只要你想要数据添加这个

  NSMutableDictionary * dataFromParent = [self.delegate giveMeData]; 

parentVC将运行'giveMeData'并返回一个NSMutableDictionary(调整所需要的数据) p>

How can I passing data from parent view controller to child view controller?

I tried doing this with delegates. but I don't think it's an option?

解决方案

Option 1

If you're passing from a parent to a child view controller, then just set the property of your childViewController from the parent.

customChildViewController.someRandomProperty = @"some random value";
[self presentViewController:customChildViewController animated:YES completion:nil];

Option 2

Or, you can set up a delegate

Step 1: set up protocol above interface in ChildViewController.h file

@protocol ChildViewControllerDelegate

- (NSDictionary *) giveMeData;

@end

@interface  .....

Step 2: create delegate property in ChildViewController.h interface

@property (strong, nonatomic) id<ChildViewControllerDelegate>delegate

Step 3: declare delegate protocol in ParentViewController.h

@interface ParentViewController : UIViewController <ChildViewControllerDelegate>

Step 4: in ParentViewController.m add this method:

- (NSDictionary *) giveMeData {
    NSMutableDictionary * dataToReturn = [[NSMutableDictionary alloc]init];
    dataToReturn[@"some"] = @"random";
    dataToReturn[@"data"] = @"here";
    return dataToReturn;
}

Step 5: before launching childViewController declare delegate property.

childViewController.delegate = self;
[self presentViewController:childViewController animated:YES completion:nil];

Step 6: in child view controller whenever you want data add this

NSMutableDictionary * dataFromParent = [self.delegate giveMeData];

The parentVC will run 'giveMeData' and return a NSMutableDictionary (adjust for whatever data you want)

这篇关于如何将数据从父视图控制器传递到子视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆