简体   繁体   English

控制器之间的通讯iOS

[英]Communication between controllers iOS

first of all I want to show you my storyboard: 首先,我想向您展示我的故事板:

在此处输入图片说明

I have a Label in the CUVCViewController (the first view of the left) and a UISlider in the CUVCViewControllerPageTwo (the second view). 我在CUVCViewController中有一个Label(左侧的第一个视图),在CUVCViewControllerPageTwo中有一个UISlider(第二个视图)。 When I tap on the "page 2" button I can change the value of the UISlider. 当我点击“第2页”按钮时,我可以更改UISlider的值。 I want to show the value of the UISlider in the label, but I don't understand how I can do it! 我想在标签中显示UISlider的值,但我不知道该怎么做! I have tried with delegation, but I don't understand how I can implement it! 我已经尝试过使用授权,但是我不知道该如何执行!

Can someone help me? 有人能帮我吗? Thanks!! 谢谢!!

Ok, so this one is pretty easy, but I'm really not sure how far you've got! 好的,这很简单,但是我真的不确定您走了多远! I'm going to assume that you just want to know how to pass the data from the UISlider back to the first view controller. 我假设您只是想知道如何将数据从UISlider回第一个视图控制器。

In the class of the first view controller, you need to implement the method 在第一个视图控制器的类中,您需要实现该方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

This is called just before the segue to the second view controller. 在第二个视图控制器被选中之前调用此方法。 you can then set your first view controller as a delegate to the second. 然后,您可以将第一个视图控制器设置为第二个的控制器。 So you would need to set up a @property on the second view controller of type 'first view controller' and call it, say, delegate (you could give it any name) 因此,您需要在“第一个视图控制器”类型的第二个视图控制器上设置@property并调用它,例如, delegate (您可以给它命名)

In your -prepareForSegue: method, do this 在您的-prepareForSegue:方法中,执行此操作

SecondViewController *secondController = segue.destinationViewController; secondController.delegate = self ; secondController.delegate = self ;

Then from this point you can refer to your first view controller as delegate from your second. 然后从这个点,你可以参考你的第一个视图控制器为delegate从你的第二个。 Prehaps you could pass the data back from the UISlider via a method call on the first, or by setting a property on the first 可能您可以通过第一个方法调用或通过在第一个方法上设置属性从UISlider传回数据

eg [delegate sliderDidUpdate:sliderValue] 例如[delegate sliderDidUpdate:sliderValue]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM