Accessing a label from another viewcontroller

Discussion in 'iOS Programming' started by polarbear505, Mar 17, 2017.

  1. polarbear505 macrumors newbie

    polarbear505

    Joined:
    Mar 17, 2017
    Location:
    Washington, DC
    #1
    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!

    Cheers!
     
  2. mds1256 macrumors regular

    Joined:
    Apr 9, 2011
    #2
    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
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    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.
     
  4. polarbear505 thread starter macrumors newbie

    polarbear505

    Joined:
    Mar 17, 2017
    Location:
    Washington, DC
    #4
    Can you expand on that more? Im a bit new to this...
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    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?
     
  6. polarbear505 thread starter macrumors newbie

    polarbear505

    Joined:
    Mar 17, 2017
    Location:
    Washington, DC
    #6
    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
     
  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    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.
     
  8. polarbear505 thread starter macrumors newbie

    polarbear505

    Joined:
    Mar 17, 2017
    Location:
    Washington, DC
    #8
    Thanks for the answer! I'm utterly stupid (and also new to this) - so I need this to be translated to english. thx
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    I suppose you need it to be translated to swift.

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

    Good luck.
     
  10. polarbear505 thread starter macrumors newbie

    polarbear505

    Joined:
    Mar 17, 2017
    Location:
    Washington, DC
    #10
    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?
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    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.
     
  12. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #12
    Don't forget to set the delegate in your prepareForSegue...
     
  13. polarbear505 thread starter macrumors newbie

    polarbear505

    Joined:
    Mar 17, 2017
    Location:
    Washington, DC
    #13
    I don't understand how to make the delegate, or prepareForSegue
     
  14. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #14
    this is done with a protocol
    prepareForSegue passes info from a to b
    to pass info from b to a you need a delegate
     
  15. polarbear505 thread starter macrumors newbie

    polarbear505

    Joined:
    Mar 17, 2017
    Location:
    Washington, DC
    #15
    But the popup does not dismiss via segue... It is a line of code; check the image in the original post
     
  16. bjet767 macrumors 6502a

    Joined:
    Oct 2, 2010
    #16
    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.
     
  17. polarbear505 thread starter macrumors newbie

    polarbear505

    Joined:
    Mar 17, 2017
    Location:
    Washington, DC
    #17
    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
     
  18. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #18
  19. bjet767, Mar 19, 2017
    Last edited: Mar 19, 2017

    bjet767 macrumors 6502a

    Joined:
    Oct 2, 2010
    #19
    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.
     
  20. polarbear505, Mar 19, 2017
    Last edited: Mar 19, 2017

    polarbear505 thread starter macrumors newbie

    polarbear505

    Joined:
    Mar 17, 2017
    Location:
    Washington, DC
    #20
    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
     
  21. bjet767 macrumors 6502a

    Joined:
    Oct 2, 2010
    #21
    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.
     

Share This Page