简体   繁体   English

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

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

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 选项1

If you're passing from a parent to a child view controller, then just set the property of your childViewController from the parent. 如果您从父视图传递到子视图控制器,那么只需从父视图设置childViewController的属性。

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

Option 2 选项2

Or, you can set up a delegate 或者,您可以设置委托

Step 1: set up protocol above interface in ChildViewController.h file 步骤1:在ChildViewController.h文件中设置上面的接口协议

@protocol ChildViewControllerDelegate

- (NSDictionary *) giveMeData;

@end

@interface  .....

Step 2: create delegate property in ChildViewController.h interface 第2步:在ChildViewController.h接口中创建委托属性

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

Step 3: declare delegate protocol in ParentViewController.h 第3步:在ParentViewController.h中声明委托协议

@interface ParentViewController : UIViewController <ChildViewControllerDelegate>

Step 4: in ParentViewController.m add this method: 第4步:在ParentViewController.m中添加此方法:

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

Step 5: before launching childViewController declare delegate property. 第5步:在启动childViewController声明委托属性之前。

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

Step 6: in child view controller whenever you want data add this 步骤6:在子视图控制器中,只要您想要数据添加此项

NSMutableDictionary * dataFromParent = [self.delegate giveMeData];

The parentVC will run 'giveMeData' and return a NSMutableDictionary (adjust for whatever data you want) parentVC将运行'giveMeData'并返回NSMutableDictionary(根据您想要的任何数据进行调整)

您可以通过以下方式访问子视图控制器:

NSArray *children = self.childViewControllers;

暂无
暂无

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

相关问题 将数据从父视图控制器传递到子视图控制器Swift 4 - Passing Data From Parent View Controller to Child View Controller Swift 4 如何从子视图控制器访问父视图控制器的视图? - how can i access a parent view controller's view from a child view controller? 如何从嵌入在父视图控制器的子视图控制器中的 UISearchViewController 呈现视图控制器? - How do I present a view controller from UISearchViewController embedded in a child view controller of a parent view controller? 如何将数据从子视图传递给父视图控制器? 在迅速 - How to pass data from child to parent view controller? in swift 如何将数据从父视图控制器传递到子容器视图控制器 - How to pass data from parent view controller to child container view controller Swift 将数据从子视图发送到父视图控制器 - Swift sending data from child view to parent view controller 如何快速从父视图控制器访问容器视图中的数据? - How can I access data from a container view from the parent view controller in swift? 我如何将数据传递回父视图控制器。 正在使用addSubView方法显示子视图 - How can i pass data back to parent view controller. am using addSubView method to show child view 将数据从一个Tab Bar View Controller的子级(我认为?)传递到另一个TabBarVC的Collection View? (编程) - Passing data from child (I think?) of one Tab Bar View Controller to Collection View of another TabBarVC? (Programmatically) 如何添加一个已经有父 controller 的视图 controller 作为另一个视图 controller 的子视图 - How do I add a view controller that already has a parent controller as a child of another view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM