-1

I am wanting to take these textfields and preform an equation between them. What I am having trouble with is automatically updating the final textfield after my equation is done

In this scenario, I want the Total Cash Price to be updated when the Vehicle Price is updated or the Service Contract is updated without having to press the DP Button.

Can someone give me a suggestion on how to accomplish this.

Code:

@IBAction func DPButtonPressed(_ sender: Any) {
        totalCashPrice.integerValue = vehiclePrice.integerValue + serviceContract.integerValue + salesTax.integerValue
}

Instead of it being @IBOutlet func DPButtonPressed(_sender: Any), i want that function to be automatically done.

enter image description here

This is where i am stuck at with using the delegate

 override func viewDidLoad() {
    super.viewDidLoad()
    totalCashPrice.delegate = self

}
func textFieldDidBeginEditing(textField: NSTextField!) {

}
func textFieldShouldEndEditing(textField: NSTextField!) -> Bool {
    return false
}
func textFieldShouldReturn(textField: NSTextField!) -> Bool {
    totalCashPrice.resignFirstResponder()
    return true
}

@IBAction func DPButtonPressed(_ sender: Any) {
    totalCashPrice.integerValue = vehiclePrice.integerValue + 
serviceContract.integerValue + salesTax.integerValue
    textFieldShouldEndEditing(textField: totalCashPrice)

enter image description here

  • could you put some code along with the question , will help to fix this – Shobhakar Tiwari Jan 05 '20 at 17:02
  • you should use textfield delegate , it will be automatially fire based on the event , check delegate – Shobhakar Tiwari Jan 05 '20 at 17:19
  • Can you provide me with an example, i am new to coding and I’ve added the delegate but i don’t even know where to begin with the code. And i very much appreciate the responses to this point so thank you – Joshua Stewart Jan 05 '20 at 17:27
  • https://stackoverflow.com/a/24604090/3400991, see this link and set the textfield delegate and from code use textfielddidend method where it will calll automatically once you done it and press return keyboard button – Shobhakar Tiwari Jan 05 '20 at 17:32

1 Answers1

0

Cocoa is event driven. Pick an event and let it drive you.

You just need to decide what kind of signal you want to respond to and configure things so that you get that signal. It’s simply a matter of specifying to yourself what “when the Vehicle Price is updated or the Service Contract is updated” means, ie what sort of user action counts as updating the field.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • You are a savior! I have probably put 5 hours of research in just for this one thing and your first bullet point is what i have been looking for this entire time! Thank you so much. A nice simple solution – Joshua Stewart Jan 05 '20 at 21:06
  • The iOS 13 way, however, would be to use the second way (the text did change notification) along with the Combine framework to establish a publish-and-subscribe pipeline that combines the contributing text fields to set the result text field / label. – matt Jan 05 '20 at 21:38
  • there are not many tutorials or helpful places for xcode Mac OS i appreciate the help for sure – Joshua Stewart Jan 06 '20 at 00:18