ioS - Simple Calculator

 1" Learning IOS (Swift) development for the first time

 

The first UI interface program: a simple calculator, mainly familiar with the basic syntax of swift, and used to other languages, so it is easy to make mistakes when writing code, but fortunately, the xcode compiler is very strong, which is worse than android in terms of interface layout Far away, it is estimated that I have not really learned his principles and used some existing components for auxiliary development.

 

Main features of this program:

 1 First, make the interface layout, drag it yourself, and artificially calculate the size of the control

 2 Bind interface components and add events in the controller.

 3. Specific calculation, input, and related business are organized into specific classes for implementation.

 

The specific picture is as follows



 

 

Relevant code:

 

controller:

 /*
    Init ui widget and add event
    */
    func initUI() {
        label = self.view.viewWithTag(100) as! UILabel
        btnCalc = self.view.viewWithTag(99) as! UIButton
        btnBack = self.view.viewWithTag(98) as! UIButton
        btnClear = self.view.viewWithTag(97) as! UIButton
       
        btnCalc.addTarget(self, action: Selector("calcResult"), forControlEvents: .TouchUpInside)
       
        btnClear.addTarget(self, action: Selector("clickClearUI"), forControlEvents: .TouchUpInside)
       
        btnBack.addTarget(self, action: Selector("clickBackUI"), forControlEvents: .TouchUpInside)
       
        // operator add event
        for a in 80...83 {
            let btn  = self.view.viewWithTag(a) as! UIButton
            btn.addTarget(self, action: Selector("clickOpt:"), forControlEvents: .TouchUpInside)
           
        }
       
        for b in 1...10 {
            let v = self.view.viewWithTag(b)
            if v != nil && v?.isKindOfClass(UIButton) == true {
                let btn = v as! UIButton
                btn.addTarget(self, action: Selector("clickNum:"), forControlEvents: .TouchUpInside)
            }
           
        }
       
       
       
       
    }

 

Business implementation class:

//
//  CalcModel.swift
//  calculator
//
//  Created by mjp on 16/1/20.
//  Copyright © 2016年 mjp. All rights reserved.
//

import Foundation

class CalcModel{
    var opt0 : String = ""
   
    var opt1: String = ""
   
    var opt: Int = -1
    /*
     Input data from ui data
    */
    func inputData(var input : Int){
        if input >= 10 {
            input = 0
        }
       
        if opt  != -1 {
            opt1.appendContentsOf(String(input))
        }else{
            opt0.appendContentsOf(String(input))
        }
    }
   
    /*
     Input operator (+ - * /) mappings(80 81 82 83)
    */
    func inputOpt(input : Int){
        self.opt = input
    }
   
    /*
     Calc result
    */
    func calc() -> String {
        var rb : String = String("")
        let op0 : Int? = Int(opt0)
        let op1 : Int? = Int(opt1)
        if op0 != nil && op1 != nil  && opt != -1{
           
            switch opt{
            case 80 :
                // +
                let t = op0! + op1!
                rb = "\(t)"
            case 81 :
                // -
                let t = op0! - op1!
                rb = "\(t)"
            case 82 :
                // *
                let t = op0! * op1!
                rb = "\(t)"
            case 83 :
                 // /
                if op1 == nil || op1 != 0 {
                    rb = "0"
                } else {
                    let t = op0! / op1!
                    rb = "\(t)"
                }
            default:
                rb = ""
               
            }

        }
       
        return rb
    }
   
    /*
    Reset data and operator
    */
    func clear() {
        opt = -1
        opt0 = ""
        opt1 = ""
    }
   
    /*
    Back data and operator
    */
    func back(){
        if opt == -1 {
            if opt0.characters.count == 0{
                let b = opt0.characters.count
                if  b > 0 {
                    let index = opt0.startIndex.advancedBy(b - 1) //swift 2.0+
                    opt0.removeAtIndex(index)
                   
                }
             }
        } else if opt >= 80 && opt <= 83 {
            if opt1.characters.count > 0 {
                let b = opt1.characters.count
                if  b > 0 {
                    let index = opt1.startIndex.advancedBy(b - 1) //swift 2.0+
                    opt1.removeAtIndex(index)
                   
                }
            } else {
                 opt = -1
            }
        } else {
            clear()
        }
    }
   
    /*
    Show input datas
    */
    func showLabel() -> String {
        let flag = self.opt
        var result = ""
            switch flag {
            case -1 :
                result = "\(opt0)"
            case 80 :
                result =  "\(opt0)+\(opt1)"
            case 81 :
                result =  "\(opt0)-\(opt1)"
            case 82 :
                result =  "\(opt0)*\(opt1)"
            case 83 :
                result =  "\(opt0)/\(opt1)"
            default :
                result =  ""
            }
            return result
      
    }
}

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327058640&siteId=291194637