简体   繁体   English

iOS容器视图和父ViewController通信?

[英]iOS Container View and Parent ViewController communication?

How can I communicate with container view controller and parent view controller? 如何与容器视图控制器和父视图控制器通信? I've tried to make a communication and pass the value between them. 我试图进行沟通并在它们之间传递价值。 I've added a container view in the green view controller. 我在绿色视图控制器中添加了一个容器视图。 And I had set segue name with "communicateSegue". 我将segue名称设置为“communicSegue”。 I've searched some methods, I know we can set the prepareForSegue to pass the value at initial. 我搜索了一些方法,我知道我们可以设置prepareForSegue来传递初始值。

 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     if ([segue.identifier isEqualToString:@"communicateSegue"]) {
         NSLog(@"call prepare for segue at parent view controller");
     }
 }

But now I want to pass value and change the label in the different viewcontroller . 但现在我想传递值并更改不同viewcontroller中的标签。 Not at the initial. 不在最初。

I don't know how to do? 我不知道该怎么办?

Question: 题:

Have anyone know how to implement ParentViewController pass value to Container View Controller and show the label in the ContainerViewController? 有谁知道如何实现ParentViewController传递值到Container View Controller并在ContainerViewController中显示标签?

And reverse , Container View Controller click the button then pass the value and change the label show in the ParentViewController with objective-c? 反之,Container View Controller单击按钮然后传递值并使用objective-c更改ParentViewController中的标签show?

Thanks. 谢谢。

My declare variable in parentViewController.h: #import 我在parentViewController.h中声明变量:#import

 @interface ParentViewController : UIViewController
 @property (weak, nonatomic) IBOutlet UITextField *parentTF;
 @property (weak, nonatomic) IBOutlet UIButton *passValToChildVCBtn;

 @property (weak, nonatomic) IBOutlet UILabel *showContainerValLB;
 @property (weak, nonatomic) IBOutlet IBAction *passValueToContainerEvent;

 @end

In the ContainerChildViewController.h #import 在ContainerChildViewController.h #import中

 @interface ContainerChildViewController : UIViewController
 @property (weak, nonatomic) IBOutlet UILabel *showParentValLB;
 @property (weak, nonatomic) IBOutlet UITextField *childTF;
 @property (weak, nonatomic) IBOutlet UIButton *passToParentBtn;
 @property (weak, nonatomic) IBOutlet IBAction *passValueToParentBtnEvent;

 @end

在此输入图像描述

test project in the here . 测试项目在这里

thank you very much. 非常感谢你。

Regarding passing value to the containerView controller, you can make a property in the ChildViewController of the value you want it to be passed. 关于将值传递给containerView控制器,您可以在ChildViewController中创建一个您想要传递的值的属性。 Then in your ParentViewController do something like the following: 然后在您的ParentViewController中执行以下操作:

self.ChildViewController.yourProperty = yourValue

The opposite can be done in 4 ways: 相反可以通过4种方式完成:

  • You can make a delegate protocol to communicate the data between your controllers. 您可以创建委托协议以在控制器之间传递数据。
  • You can post a notification in your ChildViewController and add the parent controller as an observer. 您可以在ChildViewController中发布通知,并将父控制器添加为观察者。
  • You can use KVO. 你可以使用KVO。
  • And the easiest way out, you can make a property in your parentviewController and access it like the following 最简单的方法是,您可以在parentviewController中创建一个属性,并像下面一样访问它

     ((YourParentViewControllerClassType *)self.parentViewController).yourParentProperty = TheValueYouWant; 

I do it like this: 我是这样做的:

  1. set identifier for segue from your containerView to viewController 从您的containerView到viewController设置segue的标识符
  2. make property/variable myChildViewController in your parent view controller and property/variable myParentViewController in your child view controller 在父视图控制器中创建属性/变量myChildViewController,在子视图控制器中创建属性/变量myParentViewController
  3. in your prepareForSegue method set this properties to be the reference to right objects 在prepareForSegue方法中,将此属性设置为对对象的引用
  4. Now you can access to properties of parent/child view controller through this references. 现在,您可以通过此引用访问父/子视图控制器的属性。

Example in Swift: Swift中的示例:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "YourIdentifier" {

        // Set references
        myChildViewController = segue.destinationViewController as! YourChildViewController
        myChildViewController.myParentViewController = self
    }
}

Example in Objective C 目标C中的示例

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     if ([segue.identifier isEqualToString:@"YourIdentifier"]) {

         // Set references
         myChildViewController = [segue destinationViewController]; 
         myChildViewController.myParentViewController = self
     }
 }

I did't try the objective C code becouse I don't ever use it. 我没有尝试目标C代码因为我从来没有使用它。 It is just an example to get the idea. 这只是一个实现这个想法的例子。

After that you can set any property of parent from child view controller as: 之后,您可以将子视图控制器中父项的任何属性设置为:

myParentViewController.property = value

and in your parent view controller: 并在您的父视图控制器中:

myChildViewController.property = value

Hope this helps anyone! 希望这有助于任何人!

Is pretty simple, the UIStoryboardSegue contains a reference to your destination view controller using the property -destinationViewController . 非常简单, UIStoryboardSegue包含使用-destinationViewController属性对目标视图控制器的-destinationViewController
In the method -prepareForSegue you can access it and pass the data that you need. 在方法-prepareForSegue您可以访问它并传递所需的数据。
If you need to pass data from child to parent, I strongly suggest you to avoid hard coupling and use something such as delegation or notification pattern. 如果您需要将数据从子节点传递给父节点,我强烈建议您避免硬耦合并使用委托或通知模式等内容。
Remember that your parent view controller already contains a reference to you child view controllers and you can simply access it by using -childViewControllers property. 请记住,您的父视图控制器已经包含对子视图控制器的引用,您可以使用-childViewControllers属性来访问它。

you can use NSNotificationCenter for updating all screens or you can do this way.. 您可以使用NSNotificationCenter更新所有屏幕,或者您可以这样做..
create a property in ContainerChildViewController.h ContainerChildViewController.h创建一个属性

 @property(nonatomic,copy)NSString * mytextString;

this property will be accessible in your ParentViewController.h if you import ContainerChildViewController.h 如果导入ContainerChildViewController.h则可以在ParentViewController.h访问此属性
you have to create a NSString in ParentViewController.h 你必须在ParentViewController.h创建一个NSString
in this you can set button action to copy textfielddata.text then you can assign this value to your ContainerChildViewcontroller's as 在这里你可以设置按钮动作来复制textfielddata.text然后你可以将这个值分配给你的ContainerChildViewcontroller's

// create an instance of 2nd view controller as second in ParenViewController //在ParenViewController创建第二个视图控制器的实例作为第二个

UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
   ContainerChildViewController * second= [mystoryboard instantiateViewControllerWithIdentifier:@"ContainerChildViewController"];
        second.mytextString = self.mytextString;
// either you directly pass this value or use the string it should work both ways
        NSLog(@"%@",second.orderID);
        [self.navigationController pushViewController:second animated:YES];


Reply if it works 如果有效,请回复

try this 尝试这个

- (IBAction)showDetail:(UIButton *)sender {

        DetailViewController *detailVc = [self.childViewControllers firstObject];
        detailVc.lable.text = sender.titleLabel.text;

}  

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

相关问题 获取容器视图的父ViewController - Get container view's parent viewcontroller iOS通讯ViewController->模型 - iOS communication ViewController -> Model 当在父viewController中点击按钮时,容器视图将丢失textField值 - Container view losing textField values when a button is tapped in the parent viewController 具有3个子项的Container ViewController必须具有公共父视图控制器错误 - Container ViewController with 3 children must have a common parent view controller error iOS与ViewController之间的iOS通信 - iOS Communication between Model and ViewController iOS-更改连接到视图容器中单元格的ViewController顶部栏的标题 - iOS - Change title for top bar in a viewcontroller that is connected to a cell in a view container ViewController及其视图之间的通信 - Communication between A ViewController and its View 论View与ViewController的责任与沟通 - On View and ViewController's responsibility and communication (iOS)音频播放器作为应用程序中所有父视图控制器上的子视图控制器 - (iOS) Audio player as child viewcontroller on all parent view controllers in the app 如何将参数从容器视图的父级ViewController传递到该容器视图的PageViewController? - How pass parameter from parent ViewController of a container view to PageViewController of that container view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM