0

I have an app with several views. Taking into consideration the large main view, called MyView1, it is controlled by MyView1Controller. Within MyView1, there is a button that causes a modal segue to another view, whose controller is also MyView1Controller. This modal view has a couple UILabels, and a button that terminate the modal view, bringing the user back to MainView1.

Here is the problem... Let's say in my modal view there is a UILabel called sampleLabel. While in MyView1, a button is pressed, which executes the code:

sampleLabel.text = @"changed";

Since the UILabel named sampleLabel is not on screen for MyView1, and instead is part of the modal view from MyView1, nothing happens. However, when I click on the button to view the modal view from MyView1, the UILabel hasn't changed.

This is even more puzzling since the main MyView1 and the modal view that segues off of MyView1 are controlled by the same view controller, MyView1Controller.

Can someone please tell me how I can make code that executes during the user's interaction with MyView1 change things in the modal view, so that when they press the button and segue to the modal view, the UILabel's have already been changed?

Thanks!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
jake9115
  • 3,964
  • 12
  • 49
  • 78

2 Answers2

0

First of all, Apple recommends (and it makes life a lot easier) to have one view controller for each view. So you should have a second view controller. In the second view controller you would have a property called sampleLabel. In the first view controller you could use different methods to set the sampleLabel.text. I would probably create a separate sampleLabelText property in the first view controller (could be an NSString *) and set it to the text you want when the user presses a button. Then in your

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

you would get your second view controller and set its property like this:

SecondViewController *svc = [segue destinationViewController];
svc.sampleLabel.text = self.sampleLabelText;

That's it. Hope this helps.

Jamie
  • 5,090
  • 28
  • 26
0

So I have had a similar issue that I resolved through 'delegation' but not through a segue schema. Here is a link to the stackoverflow question and my answer. Delegation

Hopefully this gets you going in the right path. Instead of modally presenting a view, I push a new viewcontroller onto a navigation stack but the same answer should apply, hopefully :P

Community
  • 1
  • 1
jsetting32
  • 1,632
  • 2
  • 20
  • 45