Protocol Not Preparing Data

I have an issue trying to transfer a number (numText) in a textField. When I type a number it prints 0.

Code Block
func sendTypedNumToParent() {
    if anotherNum != nil {
      let data = numText
      anotherNum?.sendTyped(data: data)
      print("this is the typed number being transferred \(data)")
    }
  }


I know I have the number when I enter it because I can print it in my return function. What is the issue here?

Code Block
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    let text = textField.text
    let numText = originalNum
     
    if revNumber.text == text {
      revSlider.value = Float(numText) / 6.75675675676
      revHeight.constant = CGFloat(revSlider.value)
      if numText < 316 {
        revLabel.text = ""
      } else {
        revLabel.text = "Revenue"
      }
    }
print("I typed this \(numText)")
    sendTypedNumToParent()
    let name = Notification.Name(rawValue: typedCal)
    NotificationCenter.default.post(name: name, object: nil)
    return true
  }



Answered by ZoneX in 650495022
I found my mistake I was trying to transfer numText instead of originalNum.

Code Block
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
      // Make sure the user entered a number
    let num = Double(textField.text!)
    originalNum = Int(textField.text!)!
    if num! < 0 {
        textField.text = "0"
    } else if num! > 1000 {
        textField.text = "1000"
    } else if num! >= 0 || num! <= 1000 {
        textField.text = String(Int(num!))
    }
    return true
  }


Code Block
let numText = originalNum

You post a notification.

Who subscribes to this notification ?
What do you do on receiving notification ?

Why do you call sendTypedNumToParent() and sendNotification ? What is the purpose of each of them ?

Please show code.

I know I have the number when I enter it because I can print it in my return function.

You mean textFieldShouldReturn(_:) by my return function?
You print the value of the local variable numText, but the numText in sendTypedNumToParent() is not the same thing.
So, " I can print it in my return function" does not guarantee you have the number.

How have you defined numText used in sendTypedNumToParent()?


Better use parameters to pass some values that you know you have.
Code Block
func sendTypedNumToParent(data: Int) {
if let anotherNum = anotherNum {
anotherNum.sendTyped(data: data)
print("this is the typed number being transferred \(data)")
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
let text = textField.text
let numText = originalNum
if revNumber.text == text {
revSlider.value = Float(numText) / 6.75675675676
revHeight.constant = CGFloat(revSlider.value)
if numText < 316 {
revLabel.text = ""
} else {
revLabel.text = "Revenue"
}
}
print("I typed this \(numText)")
sendTypedNumToParent(data: numText)
let name = Notification.Name(rawValue: typedCal)
NotificationCenter.default.post(name: name, object: nil)
return true
}


I guess the type of numText is Int. (You like forcing readers guess many things.)
If it is not, the code above needs to be modified.
Accepted Answer
I found my mistake I was trying to transfer numText instead of originalNum.

Code Block
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
      // Make sure the user entered a number
    let num = Double(textField.text!)
    originalNum = Int(textField.text!)!
    if num! < 0 {
        textField.text = "0"
    } else if num! > 1000 {
        textField.text = "1000"
    } else if num! >= 0 || num! <= 1000 {
        textField.text = String(Int(num!))
    }
    return true
  }


Code Block
let numText = originalNum

Protocol Not Preparing Data
 
 
Q