Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mds1256

macrumors regular
Apr 9, 2011
167
43
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
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 
  • Like
Reactions: grandM

polarbear505

macrumors newbie
Original poster
Mar 17, 2017
11
0
Washington, DC
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.
Can you expand on that more? Im a bit new to this...
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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?
 

polarbear505

macrumors newbie
Original poster
Mar 17, 2017
11
0
Washington, DC
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?
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
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
OK, so both scenarios come into play.

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.
 

polarbear505

macrumors newbie
Original poster
Mar 17, 2017
11
0
Washington, DC
OK, so both scenarios come into play.

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.
Thanks for the answer! I'm utterly stupid (and also new to this) - so I need this to be translated to english. thx
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
I suppose you need it to be translated to swift.

Do it step by step. Then come back with questions.

Good luck.
 

polarbear505

macrumors newbie
Original poster
Mar 17, 2017
11
0
Washington, DC
OK, so both scenarios come into play.

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.
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?
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

bjet767

Suspended
Oct 2, 2010
967
319
Don't over complicate your views.

Use segues to link the views via buttons or navigation controllers. Save your "pop ups" for sub views of the main view of your view controller.

Data can be passed multiple ways between views but do your best to keep it simple.

Are you using Delphi to build your code? If so I suggest using Xcode directly and learn the iOS API so you can design more iOS compliant designs.
 

polarbear505

macrumors newbie
Original poster
Mar 17, 2017
11
0
Washington, DC
Don't over complicate your views.

Use segues to link the views via buttons or navigation controllers. Save your "pop ups" for sub views of the main view of your view controller.

Data can be passed multiple ways between views but do your best to keep it simple.

Are you using Delphi to build your code? If so I suggest using Xcode directly and learn the iOS API so you can design more iOS compliant designs.
No. xcode. also its a tabbed based application, so if i dont use popups, everything will be screwed heres the storyboard http://imgur.com/gallery/TkPSj
 

bjet767

Suspended
Oct 2, 2010
967
319
No. xcode. also its a tabbed based application,

Polarbear, first xcode is the official Apple programming suite for both iOS and OS X (i products and Mac).

Second a tab based application is using the UITabBarController to move around its views.

Third I looked at your storyboard and it looks like your segue from "test" viewcontroll to "Creat Test" view controller is activated via a button.

Your "test" viewcontroller should have a "prepareForSegue" method which you personally can write if the code generator you use hasn't.

I assume you're using swift so here's some sample code:
Code:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Make sure your segue name in storyboard is the same as this line
    if (segue.identifier == "YOUR_SEGUE_NAME_HERE") {
            // Get reference to the destination view controller
        var vc: YourViewController? = segue.destination
        // Pass any objects to the view controller here, like...
        vc?.myObjectHere = object
    }
}

Now you will need to know some other things.

"YOUR_SEGUE_NAME_HERE" is the name you give the segue.

"myObjectHere" is the public label variable in the "creat test" viewcontroller.

You can now pass the data to the label in the new view controller.

BTW you best friend for questions about coding for iOS is "https://stackoverflow.com"

And if you want to convert ObjectiveC to Swift use "https://objectivec2swift.com/#/home/converter/"

Here's another example:

Code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
       var DestViewController : SecondViewController = segue.destinationViewController as! SecondViewController

       DestViewController.TotalMoneyLabel.text = T1Total
   }

Are you using Delphi to design you layout? You really need to LEARN Xcode and Swift or ObjectiveC if you want to master iOS programming or simple questions like this will continue to stump you.



---------------

You might also consider saving you "test" data in the user defaults file of your app.

here's some sample code:

Code:
override init() {
    super.init()
   
    defaults = UserDefaults(suiteName: "your.group.com")
    //now get any that are stored
    self.getDefaults()
    //save any defaults not set
    self.setDefaults()

}

func getDefaults() {
    data1 = defaults.float(forKey: kDataOne)
}

func setDefaults() {
    defaults.set(data1, forKey: kDataOne)
}

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.
 
Last edited:

polarbear505

macrumors newbie
Original poster
Mar 17, 2017
11
0
Washington, DC
Polarbear, first xcode is the official Apple programming suite for both iOS and OS X (i products and Mac).

Second a tab based application is using the UITabBarController to move around its views.

Third I looked at your storyboard and it looks like your segue from "test" viewcontroll to "Creat Test" view controller is activated via a button.

Your "test" viewcontroller should have a "prepareForSegue" method which you personally can write if the code generator you use hasn't.

I assume you're using swift so here's some sample code:
Code:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Make sure your segue name in storyboard is the same as this line
    if (segue.identifier == "YOUR_SEGUE_NAME_HERE") {
            // Get reference to the destination view controller
        var vc: YourViewController? = segue.destination
        // Pass any objects to the view controller here, like...
        vc?.myObjectHere = object
    }
}

Now you will need to know some other things.

"YOUR_SEGUE_NAME_HERE" is the name you give the segue.

"myObjectHere" is the public label variable in the "creat test" viewcontroller.

You can now pass the data to the label in the new view controller.

BTW you best friend for questions about coding for iOS is "https://stackoverflow.com"

And if you want to convert ObjectiveC to Swift use "https://objectivec2swift.com/#/home/converter/"

Here's another example:

Code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
       var DestViewController : SecondViewController = segue.destinationViewController as! SecondViewController

       DestViewController.TotalMoneyLabel.text = T1Total
   }

Are you using Delphi to design you layout? You really need to LEARN Xcode and Swift or ObjectiveC if you want to master iOS programming or simple questions like this will continue to stump you.



---------------

You might also consider saving you "test" data in the user defaults file of your app.

here's some sample code:

Code:
override init() {
    super.init()
 
    defaults = UserDefaults(suiteName: "your.group.com")
    //now get any that are stored
    self.getDefaults()
    //save any defaults not set
    self.setDefaults()

}

func getDefaults() {
    data1 = defaults.float(forKey: kDataOne)
}

func setDefaults() {
    defaults.set(data1, forKey: kDataOne)
}

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.
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
 
Last edited:

bjet767

Suspended
Oct 2, 2010
967
319
OK

I guess it's a communication issue, sorry.

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.

Keep pluging/typing away.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.