swiftdoubleuistoryboardseguensintegeruistepper

how to use uistepper to multiply value from segue data transfer


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.


Solution

  • 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
    }