0

EDIT Current Code

So I am just starting iOS development so I decided to make a Tip Calculator to learn the basics and the foundation of Swift. I am 70% way there I feel like however I am having trouble with referencing data from another UIViewController...

This is what my story board looks like.....

So I have the load screen which after I click the button it goes to the Tip Calculator...then I have a settings which can change the titles of the segmented control sections. So if I wanted to change my default settings to (10%, 15%, 30%) I can do that on Settings. However I can not seem to figure out how to do get the TextField.text and making it the title of the Segmented Control title.

This is the code for the Tip Calculator (2nd screen)

I have a class ViewController3 class that belongs to the Settings page but it is completely blank.

Jay Park
  • 3
  • 4
  • Are you trying to pass data to the next viewController? – Ahmad F Nov 27 '16 at 19:32
  • Im trying to get the data from the textFields in "Settings" to change the titles of the Segmented Control in the previous viewController – Jay Park Nov 27 '16 at 19:58
  • If you want to *previously* pass data, you should work with **delegation**. you might want to check [this answer](http://stackoverflow.com/questions/25522912/passing-data-back-from-view-controllers-xcode). – Ahmad F Nov 27 '16 at 20:01
  • @JayPark, you're a Hokie? Me too! – vacawama Nov 27 '16 at 22:21
  • @vacawama Hokies unite! Thanks for answering...I'm trying to do it through delegates but it isn't seem to be working so I'll try your solution. – Jay Park Nov 27 '16 at 22:57

1 Answers1

0

To pass the settings back to your first viewController, you can use a closure that is passed from the first viewController.

Here is an example of how to do this. In prepare(for:) set the closure processTipSettings to point to a handler function. In this case, changeSegmentLabels will take the tip settings and make them the labels of the segmented control.

When the user hits save, the @IBAction for the button will call processTipSettings with the values from the textFieldLabels, and changeSegmentLabels will update the titles in the segmented control.


ViewController.swift:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var tipsSegmentedController: UISegmentedControl!

    func changeSegmentLabels(labels: [String?]) {
        if labels.count == 3 {
            for i in 0..<3 {
                tipsSegmentedController.setTitle(labels[i], forSegmentAt: i)
            }
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "settings" {
            if let dvc = segue.destination as? SettingsViewController {
                dvc.processTipSettings = changeSegmentLabels
            }
        }
    }

}

SettingsViewController.swift

class SettingsViewController: UIViewController {

    @IBOutlet weak var tip1: UITextField!
    @IBOutlet weak var tip2: UITextField!
    @IBOutlet weak var tip3: UITextField!

    // Call this closure when Save is pressed.
    var processTipSettings: (([String?]) -> ())?

    @IBAction func saveSettings(_ sender: UIButton) {
        processTipSettings?([tip1.text, tip2.text, tip3.text])

        _ = self.navigationController?.popViewController(animated: true)
    }

}
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • What is the segue.indentifier? I assume its the title at the top of the screen right? – Jay Park Nov 27 '16 at 23:10
  • You can skip that if you only have the one segue. If you have multiple segues all from the same view controller, the segue identifier allows you to figure out which one you're looking at. You set the identifier in the Storyboard by clicking on the segue arrow and then setting the identifier string in the attributes inspector. – vacawama Nov 27 '16 at 23:12
  • Ok great thanks, do you mind adding me somewhere we can chat quickly? Cause I have a few quick fast questions..I'd appreciate it a lot – Jay Park Nov 27 '16 at 23:16
  • Ask the questions here and SO will offer for us to move it to a chat shortly. – vacawama Nov 27 '16 at 23:20
  • So I made it so processTips just uses a single text string instead of a string of Arrays. so I just took out the "[ ]" in both the initializations so it essentially looks like var processTipSettings: ((String?) -> ())? and the parameter of the changeLabels is (text: String)? however it doesnt seem to be working...the screen goes back to the original after you click Save Settings but the button title isnt changing – Jay Park Nov 27 '16 at 23:25
  • Show your `prepareForSegue`, your `processTips`, and the save code. – vacawama Nov 27 '16 at 23:35
  • In my original question, I added a screenshot of the current code – Jay Park Nov 27 '16 at 23:41
  • Make sure you are getting the the line of code that assigns the settings routine to the dvc. Add prints or breakpoints. – vacawama Nov 27 '16 at 23:54
  • Your tip amount outlets don't appear to be connected. – vacawama Nov 27 '16 at 23:59
  • I'm only doing tip1 to make sure the functionality completely works. I do processTipSettings?(tip1Amount.text) with changeLabels taking in that text and setting the 0 index to it. Unless I'm missing something completely – Jay Park Nov 28 '16 at 00:02
  • The circle next to tip1Amount is hollow which tells me the outlet is not connected. Drag from the circle to the text field in your storyboard for the tip1Amount. – vacawama Nov 28 '16 at 00:08
  • I just tried it and dragged all the amounts. However the seg control title for index 0 is still not being changed. – Jay Park Nov 28 '16 at 00:52
  • It seems that the line that assigns the dvc to ViewController3 isnt being reached either. **EDIT I just took out the if statement and it ended up working! thanks for you help!! – Jay Park Nov 28 '16 at 00:57
  • It sounds like you hadn't set the segue identifier correctly. Please accept my answer if you found it helpful. – vacawama Nov 28 '16 at 01:17