ios - UILabel value is empty when I tap a button in basic calculator code -
According to the fragment of my ordinary calc code: when I tap on number 1, then the UILB value will be 1 followed by I'll tap the button to add and the first variable will be 1 and the UILBL value and UILBlow screen will be blank (due to MyLabel.text = ""). On the other hand, the basic iOS calculator is different from working logic. When someone add a Tap button, the first variable is missing from the value label and when you change the operator or something else. The first value of the variable is still in memory, what is the logic behind it? I am searching for a lot of code in Java, but every sample is mine Please provide a clue? I recommend that you add a state variable to your calculator Are there. There are some recommended states: } Then you can keep track of what is happening with your calculator all the time. Start your calculator in the state If you are in If they are in If they are in Using an enumeration to track the status of your calculator, you can easily "do what? Meditation Give: You have never set the field to clear the field with this model @Iboutlet var MyLabel: UILabel! @abouting funk number onipad (Sender: UIButton) {If MyLabel.text == "0" {MyLabel.text = "1"} and {MyLabel.text = MyLabel.text! + "1"}} ... IBAction func addTapped (Sender: UIButton) {Operant = "add" var var1 = "\ (MyLabel.text!)" As NSString num1 = var1.doubleValue MyLabel.text = "" } @IBAction func equalTapped (Sender: AnyObject) {if operant == "add" {var num2 = ("\ (MyLabel.text!)") As NSString var2 = num2.doubleValue MyLabel.text = "\ (var1 + Var2) "}
enum calculator state {Case equivalent equal case first number Started Case Operator Justified Case Second Sequence is started
.EqualJustPressed states and user types a number, sets the field in points and switches to state
is. The first number starts . If user
. If Excel typed into Justpress , store the value of the field in
var1 , record the operator, and go to
. Operator Just Center .
. First number number state and user enters one digit, just add it to the area. If they enter an operator, then record the operator, set the state to
. Store the field value in Operator Justified and
var1 .
. Operator just-entered and they type in a number, set the field to points and switch the state to
. Second Number Starred . If they hit any other operator, then simply record the operator.
. Second Number Starred and they enter a number, just add it to the field. If they set the hit
= ,
var2 to the value of the field, calculate the result and put it in the field and set the state to
. Excel Justified . If they
. If you hit an operator while turning the number in seconds, behave like the
= case, but set the
var1 to the result and set the status
. Just entered .
"" field is always either the latest result of a calculation or it is currently entering, like a real calculator Only.
Comments
Post a Comment