简体   繁体   中英

Swift : How to update UILabel from another class X: UIButton

i'm new in the ios development and i have a question :

How to call a function from a another class to update an UILabel on my ViewController ?

Description :

Here i have un button with different UIBezierPath i add several listner of each UIBezierPath. Each UIBezierPath match an function in "viewController.swift" but i have an error when "self.ChiffreActuel.text = chif" are executed, the error is "fatal error: unexpectedly found nil while unwrapping an Optional value". I don't understand why this error appear and how to correct that.

Here is my code:

ViewController.swift :

import UIKit

class ViewController: UIViewController{

var calculeChiffre = [String]();
var chiffre="";
var update = false;
var newCalcule = false;
var diviser = false;


@IBOutlet weak var RecapeCalcule: UILabel!

@IBOutlet weak var ChiffreActuel: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func point() {
    chiffre = chiffre + ".";
    affichageResulte(chiffre);
}

func C0() {
    chiffre = chiffre + "0";
    affichageResulte(chiffre);
}
func C1() {
    chiffre = chiffre + "1";
    affichageResulte(chiffre);
}

func C2() {
    chiffre = chiffre + "2";
    affichageResulte(chiffre);
}

func C3() {
    chiffre = chiffre + "3";
    affichageResulte(chiffre);
}

func C4() {
    chiffre = chiffre + "4";
    affichageResulte(chiffre);
}

func C5() {
    chiffre = chiffre + "5";
    affichageResulte(chiffre);
}

func C6() {
    chiffre = chiffre + "6";
    affichageResulte(chiffre);
}
func C7() {

    chiffre = chiffre + "7";
    affichageResulte(chiffre);
}

func C8() {
    chiffre = chiffre + "8";
    affichageResulte(chiffre);
}

func C9() {
    chiffre = chiffre + "9";
    affichageResulte(chiffre);
}

func moins() {
    calculeChiffre.append(chiffre+" \n - \n");
    resetAffiche()
    affichageCalcule()
}

func plus() {
    calculeChiffre.append(chiffre+" \n + \n")
    resetAffiche()
    affichageCalcule()
}

func division() {
    calculeChiffre.append(chiffre+" \n / \n");
    resetAffiche()
    affichageCalcule()
    diviser=true;
}

func multiplier() {
    calculeChiffre.append(chiffre+" \n * \n");
    resetAffiche()
    affichageCalcule()

}


func egale() {

    if(diviser){
        diviser=false
         calculeChiffre.append(chiffre+".0")
    }else{
         calculeChiffre.append(chiffre)
    }
    chiffre=""
    var total = ""

    for index in 0..<calculeChiffre.count {
        total = total + calculeChiffre[index]
    }
    print(total)
    let expn = NSExpression(format:total)
    let totalFinal = expn.expressionValueWithObject(nil, context: nil)
    update = true
    newCalcule = true
    calculeChiffre.append("\n =")

    affichageResulte("\(totalFinal)")
    affichageCalcule()

    calculeChiffre.removeAll()

}

func remiseZero() {
    calculeChiffre.removeAll()
    resetAffiche()
}

func resetAffiche(){
    self.ChiffreActuel.text = "0";
    self.RecapeCalcule.text = "";
    chiffre="";
}

func affichageResulte(chif: String){

    if(update){
        update = false;
        resetAffiche()
        self.ChiffreActuel.text = chif
    }else{
        if(newCalcule){
            newCalcule = false;
            self.RecapeCalcule.text = "";
        }
        print(chif)
        self.ChiffreActuel.text = chif
    }

}

func affichageCalcule(){
    var calcule = "";

    for index in 0..<calculeChiffre.count {
        calcule = calcule + calculeChiffre[index];
    }
    self.RecapeCalcule.text = calcule

}

} 

Shape.swift

 import UIKit

@IBDesignable
class Shape: UIButton{

var bezierPath: UIBezierPath!
var bezier2Path: UIBezierPath!

let controller: ViewController = ViewController()

override func awakeFromNib() {
    addTarget(self, action: #selector(touchDown), forControlEvents: .TouchDown)
}

override func drawRect(rect: CGRect) {
    //Create several UIBezierPath

    //// Bezier Drawing
    bezierPath = UIBezierPath()
    bezierPath.moveToPoint(CGPoint(x: 0.5, y: 50.5))
    bezierPath.addLineToPoint(CGPoint(x: 30.5, y: 31.5))
    bezierPath.addLineToPoint(CGPoint(x: 63.5, y: 14.5))
    bezierPath.addLineToPoint(CGPoint(x: 100.5, y: -0.5))
    bezierPath.addLineToPoint(CGPoint(x: 123.5, y: 66.5))
    bezierPath.addLineToPoint(CGPoint(x: 90.5, y: 80.5))
    bezierPath.addLineToPoint(CGPoint(x: 58.5, y: 97.5))
    bezierPath.addLineToPoint(CGPoint(x: 41.5, y: 109.5))
    bezierPath.addLineToPoint(CGPoint(x: 0.5, y: 50.5))
    bezierPath.closePath()
    UIColor.grayColor().setFill()
    bezierPath.fill()
    UIColor.blackColor().setStroke()
    bezierPath.lineWidth = 1
    bezierPath.stroke()


    //// Bezier 2 Drawing
    bezier2Path = UIBezierPath()
    bezier2Path.moveToPoint(CGPoint(x: 45, y: 114))
    bezier2Path.addLineToPoint(CGPoint(x: 69.5, y: 98.5))
    bezier2Path.addLineToPoint(CGPoint(x: 98.5, y: 83.5))
    bezier2Path.addLineToPoint(CGPoint(x: 125, y: 73))
    bezier2Path.addLineToPoint(CGPoint(x: 148, y: 141))
    bezier2Path.addLineToPoint(CGPoint(x: 125.5, y: 150.5))
    bezier2Path.addLineToPoint(CGPoint(x: 107.5, y: 159.5))
    bezier2Path.addLineToPoint(CGPoint(x: 88, y: 172))
    bezier2Path.addLineToPoint(CGPoint(x: 45, y: 114))
    bezier2Path.closePath()
    bezier2Path.lineJoinStyle = .Round;

    UIColor.grayColor().setFill()
    bezier2Path.fill()
    UIColor.blackColor().setStroke()
    bezier2Path.lineWidth = 1
    bezier2Path.stroke()



}

func touchDown(button: Shape, event: UIEvent) {
    if let touch = event.touchesForView(button)?.first {
        let location = touch.locationInView(button)

        // Add several listner of each UIBezierPath

        if bezierPath.containsPoint(location) == true {
            print("1")
            controller.C1()
        }
        if bezier2Path.containsPoint(location) == true {
            print("2")
            controller.C2()

        }
    }

}

}

Thank you for your help.

Simply put that function in an extension of a UIView .

extension UIView {
    //function
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM