Hello, I am presenting a pop-up on my view controller, http://imgur.com/sQcPNJ6 and I want to access a label on the viewcontroller that is presenting it. (the left one) So I put this code on the pop-up (the right one) http://imgur.com/mUY4Mmn If anyone could help me, I would be very grateful!

Not sure why you are creating a new 2nd View controller when there is already one there??

So because the presenting view controller is already on the stack what you need to do is to provide a reference to the presenting viewcontroller on the pop-up view controller and then use this reference within the popup.

1. Create a new property within the pop-up view controller
2. As you present the pop-up new view controller set the new property to self from the presenting view controller into the pop-up view controller

It's usually a bad idea for one view controller to adjust the views of a second view controller. It's usually better to use a single data model object that configures the second view controller.

If you're presenting the view controller using a segue then set the data model object onto the second view controller in prepareForSegue. The second view controller should then configure itself in viewDidLoad.

If you want the second view controller to send info to the presenting view controller then you use a delegate protocol. The first view controller sets itself as the delegate and the second view controller calls the delegate methods as it needs to.

I think the code you showed won't work because the views aren't loaded yet.

It's usually a bad idea for one view controller to adjust the views of a second view controller. It's usually better to use a single data model object that configures the second view controller.

If you're presenting the view controller using a segue then set the data model object onto the second view controller in prepareForSegue. The second view controller should then configure itself in viewDidLoad.

If you want the second view controller to send info to the presenting view controller then you use a delegate protocol. The first view controller sets itself as the delegate and the second view controller calls the delegate methods as it needs to.

I think the code you showed won't work because the views aren't loaded yet.

Which one of the two scenarios I mention is the one you're using? vc1 presents vc2 using a segue and needs to configure vc2 or vc2 needs to send info back to vc1?

Click to expand...

SecondViewController presents CreateTest as a popup via segue, then when the user fills out CreateTest, the popup dismisses itself, and SecondViewController is there. I want CreateTest to update the labels in SecondViewController

CreateTest will have a delegate property and a delegate protocol. Probably you only need one method in the protocol. Something like func save(_ data: MyTest) where the MyTest class holds all the properties of the test that was just created.

SecondViewController will adopt the protocol and implement the save() function.

SecondViewController will implement prepareForSegue() and will set the delegate property on CreateTest in that method to self.

When the user taps 'Done' CreateTest builds a myTest object and calls delegate?.save(myTest) and SecondViewController reads all the information from the myTest object and sets the values of its controls based on this info.

CreateTest will have a delegate property and a delegate protocol. Probably you only need one method in the protocol. Something like func save(_ data: MyTest) where the MyTest class holds all the properties of the test that was just created.

SecondViewController will adopt the protocol and implement the save() function.

SecondViewController will implement prepareForSegue() and will set the delegate property on CreateTest in that method to self.

When the user taps 'Done' CreateTest builds a myTest object and calls delegate?.save(myTest) and SecondViewController reads all the information from the myTest object and sets the values of its controls based on this info.

Click to expand...

Thanks for the answer! I'm utterly stupid (and also new to this) - so I need this to be translated to english. thx

CreateTest will have a delegate property and a delegate protocol. Probably you only need one method in the protocol. Something like func save(_ data: MyTest) where the MyTest class holds all the properties of the test that was just created.

SecondViewController will adopt the protocol and implement the save() function.

SecondViewController will implement prepareForSegue() and will set the delegate property on CreateTest in that method to self.

When the user taps 'Done' CreateTest builds a myTest object and calls delegate?.save(myTest) and SecondViewController reads all the information from the myTest object and sets the values of its controls based on this info.

Click to expand...

So I would need to have a delegate function? My code on it now saves the test locally on the iPhone with NSUserDefaults. So I'm confused now, can you figure out what to do?

It's up to you. You can do both. You can save the values to NSUserDefaults but you need to notify the SecondViewController to update itself with the new values. That's what the delegate callback function does.

It's up to you. You can do both. You can save the values to NSUserDefaults but you need to notify the SecondViewController to update itself with the new values. That's what the delegate callback function does.

You really need to learn Xcode, Swift or ObjectiveC and the Apple iOS API. Stop relying on code generators from some cross-platform designer.

BTW I use ObjectiveC.

Click to expand...

I'm not using code generators... I use xcode. No 3rd parts apps, I said in my previous post "No. xcode" Meaning I dont use whatever delphi is; I use xcode. I've been using it ever since i started coding. Also, im in the same boat as you. I don't know swift; im using objective-c. I'll just include my project

I ran you code and what it seems you are doing is creating a data base of tests with some fields in it.

Personally I would approach it a bit different and use Document based app with an object named tests (or something like it) that is saved in an array.

I would also use a tableview to list the tests saved; similar to the "master-detail" app (from templates).

But as far as what you have done, getting the info from the the create test view back to your tests/secondviewcontroller can be handled more than one way.

Trying to keep it simple, I might do this:

If you are going to save the test to the user defualts then what you will need to do is have createtest view post a notification that the test it has created was saved and then have the tests/secondviewcontroller set up as an observer so when its view returns it will read and update the view with the created test.

The interesting thing about programming is, we all have an opinion about how to attack the problem at hand.

Oh and you're going to have to add some "constraints" so your views will properly display themselves on the different iOS screen sizes.

MacRumors attracts a broad audience
of both consumers and professionals interested in
the latest technologies and products. We also boast an active community focused on
purchasing decisions and technical aspects of the iPhone, iPod, iPad, and Mac platforms.