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? @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) "}

I recommend that you add a state variable to your calculator Are there. There are some recommended states:

  enum calculator state {Case equivalent equal case first number Started Case Operator Justified Case Second Sequence is started   

}

Then you can keep track of what is happening with your calculator all the time.

  1. Start your calculator in the state

  2. In

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

  3. If you are in . 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 .

  4. If they are in . 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.

  5. If they are in . 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 .

    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 "" field is always either the latest result of a calculation or it is currently entering, like a real calculator Only.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -