简体   繁体   English

使用prepareForSegue在视图控制器之间传递数据

[英]Passing data between View Controllers using prepareForSegue

I'm trying to create a simple project to pass some data between two view controllers which I have set up using storyboarding. 我正在尝试创建一个简单的项目,以便在我使用情节提要设置的两个视图控制器之间传递一些数据。

The project is very simple - on the first page I have a text field, and then a button takes the user to a second view controller where the text should hopefully be displayed, but as of yet no luck. 该项目非常简单-在第一页上,我有一个文本字段,然后一个按钮将用户带到第二个视图控制器,希望可以在其中显示文本,但到目前为止还没有运气。

I am using the 'Modal' style for the Segue, here is the code: 我为Segue使用了“ Modal”样式,下面是代码:

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

ViewController2 *vc2;

vc2 = [segue destinationViewController];

vc2.label.text = textField.text;    

} }

I would be grateful if someone could help me out with the correct code and explain to me why what I currently have doesn't work. 如果有人可以帮助我提供正确的代码并向我解释为什么我目前无法使用的代码,我将不胜感激。 I've searched around, and while there seem to be several variations as to how this can be done, this seems pretty much correct to me. 我四处搜寻,虽然在执行方法上似乎有多种变体,但对我来说似乎很正确。

I only moved to iOS from the console about two weeks ago, so please keep it simple! 大约两周前我才从控制台移至iOS,因此请保持简单!

Thanks in advance! 提前致谢!

Your problem (probably!) is that the destinationViewController has not yet loaded its view hierarchy at the point where you're trying to update it. 您的问题(可能是!)是destinationViewController在您尝试更新视图层次时尚未加载其视图层次结构。 You can verify this with: 您可以使用以下方法进行验证:

NSLog(@"Label: %@", vc2.label);

The way around this is to create a property in ViewController2 that is just a string and set that instead. 解决此问题的方法是在ViewController2中创建一个只是字符串的属性,然后对其进行设置。 Then, when viewDidLoad: executes in ViewController2 , copy the string property into the label's text. 然后,当viewDidLoad:执行ViewController2 ,字符串属性复制到标签的文本。

I know its ugly but try casting the destinationviewcontroller. 我知道它很丑陋,但尝试投射destinationviewcontroller。 I've tried it and it seems to help out with situations such as this. 我已经尝试过了,它似乎可以解决这种情况。

ViewController2 *vc2 = (ViewController * ) [segue destinationviewcontroller]; ViewController2 * vc2 =(ViewController *)[segue destinationviewcontroller];

Also, if you are using storyboards, are you in a uinavigationcontroller or no? 另外,如果您使用情节提要,是在uinavigationcontroller中还是没有?

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

相关问题 在View控制器之间传递数据,而无需使用prepareforsegue - Pass data between View controllers without using prepareforsegue prepareForSegue是在视图控制器之间传递值的正确方法 - Is prepareForSegue right way of passing value between view controllers 使用Segue在View Controllers之间传递数据 - Passing data between View Controllers using Segue 在不使用PrepareForSegue的情况下在segue之间传递数据 - Passing data between segues without using PrepareForSegue 使用 prepareForSegue 在 ViewControllers 之间传递数据不起作用 - Passing data between ViewControllers using prepareForSegue not working 在视图控制器之间传递数据 - Passing data between view controllers iOS Swift-使用容器视图在视图控制器之间传递数据 - IOS Swift - Passing data between view controllers using container view 使用segue在情节提要中的视图控制器之间传递数据 - Passing data between view controllers in storyboards using segue 使用协议和委托在视图控制器之间传递数据 - Passing data between view controllers using protocol and delegate 在Swift中页面视图控制器内的视图控制器之间传递数据 - Passing data between view controllers inside page view controllers in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM