Swift:如何从另一个类X:UIButton更新UILabel

时间:2016-06-10 11:16:41

标签: ios swift uibutton uilabel

我是ios开发的新手,我有一个问题:

如何从另一个类调用函数来更新ViewController上的UILabel?

说明:

这里我有不同的 UIBezierPath 的按钮我添加了每个UIBezierPath的几个列表器。 每个UIBezierPath都匹配“viewController.swift”中的一个函数,但是当执行“self.ChiffreActuel.text = chif”时我有一个错误, 错误是“致命错误:在展开可选值时意外发现nil”。 我不明白为什么会出现这个错误以及如何纠正错误。

这是我的代码:

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()

        }
    }

}

}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

只需将该功能放在UIView的扩展名中即可。

extension UIView {
    //function
}