[英]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种方式完成:
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: 我是这样做的:
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.