如何使用 uistepper 从 segue 数据传输中乘以价值

how to use uistepper to multiply value from segue data transfer

所以我遇到了 ,我想知道如何使用来自 segue 数据传输的值来做到这一点。我希望能够将我的数据传输值用作原始值,并且仅当步进器值更改时才更改该值。

例如,当步进器值增加时我的原始值为 5,10、15、20、25 ... 等等,当它减小时也是如此。我该怎么做呢?

这是我的数据传输函数:

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

这是我目的地的全局变量 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?

这是我用来连接一切的函数:

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

UIStepper 函数,我不知道该放什么:

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

现在我想知道,根据我得到的,如何使用成本值并使其成为原始值,这样当步进器值发生变化时,原始值永远不会改变。假设数据传输结束时是 5.00 美元,我只想将 5 美元乘以步进值,我不希望它改变值。

首先,您需要获得成本字符串的数字版本。

在视图控制器的顶部:

private var costDecimal : Decimal = 0

在你所谓的“数据传输”中(你可以在转场之前或之后进行——这并不重要):

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

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

(取自

现在,在你的步进器中:

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