1

So I came across this question that relates to mine and I was wondering how I can do this with a value from a segue data transfer. I want to be able to use my data transfer value as the original value and only have that value change if the stepper value changes.

For example my original value is 5 when the stepper value increases, 10, 15, 20, 25 ... and so on, and same thing for when it decreases. How can I go about doing this?

Here is my data transfer function :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == Constants.Segues.fromEventDetailsToTicketForm {
        guard let user = Auth.auth().currentUser else { return }
        let vc = segue.destination as! TicketFormViewController
        
        vc.ticketHolderName = user.displayName
        vc.costOfEvent = selectedEventCost
        vc.dateOfEvent = selectedEventDate
        vc.navigationItem.title = selectedEventName
    }
}

Here is the global variables in my destination vc:

 @IBOutlet weak var nameOfTicketHolderLabel: UILabel!
@IBOutlet weak var nameOTicketHolderTextF: UITextField!
@IBOutlet weak var numberOfGuestsLabel: UILabel!
@IBOutlet weak var dateOfEventLabel: UILabel!
@IBOutlet weak var actualDateOfEvent: UILabel!
@IBOutlet weak var totalCostOfEventLabel: UILabel!
@IBOutlet weak var actualCostOfEvent: UILabel!
@IBOutlet weak var guestNumberCount: UILabel!


var ticketHolderName: String?
var costOfEvent: String?
var dateOfEvent: String?
var nameOfEvent: String?

And this is the function I use to connect everything:

  func dataTransferVerification() {
    if let nameToLoad = ticketHolderName {
        nameOTicketHolderTextF.text = nameToLoad
    }
    
    if let costToLoad = costOfEvent {
        actualCostOfEvent.text = costToLoad
    }
    
    if let dateToLoad = dateOfEvent {
        actualDateOfEvent.text = dateToLoad
    }
    
    if let nameToLoad = nameOfEvent {
        navigationItem.title = nameToLoad
    }
}

The UIStepper func, I have no idea what to put in here:

@IBAction func guestsCount(_ sender: UIStepper) {
    guestNumberCount.text = String(Int(sender.value))
    
  
    
}

Now I want to know, with what I got, how to use the cost value and make it original so when the stepper value changes, the original value never changes. Say it's $5.00 when the data transfers over, I only want $5 to be multiplied by the stepper value, I don't want it to change value.

dsonawave
  • 137
  • 2
  • 12
  • 1
    Where are you getting stuck? Presumably you know how to make a UIStepper. You probably also know how to send a value to the new view controller in the segue. Is the issue just getting that value into the stepper? Can you show some code? – jnpdx Mar 13 '21 at 00:46
  • In the question I linked, the guy who answered had a global variable of `50` as an `Int`, when i do a data transfer I can't use that value as a global variable value, that's where I'm stuck. I deleted my code and tried another way, but that eventually didn't end up working either. I'll add my data transfer code and the global variables in my destination vc. @jnpdx – dsonawave Mar 13 '21 at 04:43
  • I don't see a UIStepper in your code at all. Seems like all you will need is an additional variable to store how many times the $5.00 should be multiplied by. – jnpdx Mar 13 '21 at 04:50
  • The thing is it won't always be $5. How can I make it dynamic? If I were to multiply the data transferred value by the stepper value it would just keep multiplying till infinity, it would be 10 then 30, 120, 600 .... so on@jnpdx – dsonawave Mar 13 '21 at 04:51
  • The cost is being passed in through `costOfEvent`? Is it really a `String`? – jnpdx Mar 13 '21 at 04:52
  • Yes it is a String indeed. @jnpdx – dsonawave Mar 13 '21 at 04:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229846/discussion-between-jnpdx-and-dsonawave). – jnpdx Mar 13 '21 at 04:55

1 Answers1

1

First, you'll want to get a numerical version of your cost string.

At the top of your view controller:

private var costDecimal : Decimal = 0

In your "data transfer" as you call it (you could do this before or after the segue -- doesn't really matter):

let formatter = NumberFormatter()
formatter.numberStyle = .currency

if let number = formatter.number(from: str) {
    let amount = number.decimalValue
    print(amount)
    self.costDecimal = amount
}

(taken from https://stackoverflow.com/a/41884823/560942)

Now, in your stepper:

@IBAction func guestsCount(_ sender: UIStepper) {
    guestNumberCount.text = String(Int(sender.value))
    //multiply sender.value by the cost that was passed in
    let totalCost = Decimal(sender.value) * costDecimal
}
jnpdx
  • 45,847
  • 6
  • 64
  • 94