0
votes

FIRST SCENARIO:

I have two view controllers

VC1 has a button, and a label

VC2 has a button, and a text field

theres a modal segue between VC1 -> VC2

when I run this segue, we set VC1, as the delegate for VC2.

We go to VC2, fill out the text field, hit the button, and VC2 is dismissed.

some delegated method is run on VC1, and VC1.label is filled in.

question: is there any way to do this without dismissing VC2.. for example, if VC2.button just modal segues us back, or slides us back to VC1 im assuming it re initializes the viewcontroller and the label wont be changed. do you always have to dismiss the view controller

SCENARIO 2:

again, two view controllers.

this time its reversed.. so i have

VC1 with a textfield and a button

VC2 with a label and a button

soo now we fill out VC1, and we expect it to show up on VC2. But without a segue, they have no relationship. is there any way to pass data between VCs using delegation without one initial segue? Is this segue requirement to use delegation something specific to view controllers? Im assuming it is because in other cases we just instantiate objects, and use their delegate methods. but with view controllers we want to reference one that is already created, and not instantiate a second one.

note: im using story boards

2

2 Answers

0
votes

Sorry, dont fully understand what you want .. but here is my take.

FIRST SCENARIO:

Why would you need to update the view that isnt on screen ?

Just update in viewWillAppear.

Otherwise you can have the delegate update it when you finish editing that textfield.

SCENARIO 2:

You need a link between the view controllers, use segues makes easy, set as delegate and pass along the info. Why make it harder than it needs to be

Many things have delegates, textfields etc, you are just saying this class / obj will do something for something else.

There are many youTubes about delegates, ie

http://www.youtube.com/watch?v=eNmZEXNQheE

For more info see this stack post - it covers everything you need to know

Passing Data between View Controllers

0
votes

1) You could do it without dismissing VC2, but it's not a good idea. You don't want to segue "back" to one, because, as you surmised, you're actually creating a new instance of VC1, and then if you segue again to VC2, you're creating a new instance of that too. You will keep piling up more and more instance of the two controllers and none will ever be deallocated.

2) Again, your instincts are correct -- you need to somehow get a reference to the instance of VC2 that your putting on screen in order to set yourself as delegate. You don't have to have a segue to do that, you could create the second controller in code and do a manual push or presentViewController, but that's functionally, the same as doing a segue.