gpt4 book ai didi

ios - swift : How to update UILabel from another class X: UIButton

转载 作者:行者123 更新时间:2023-11-30 13:21:20 34 4
gpt4 key购买 nike

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

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

描述:

这里我有一个具有不同UIBezierPath的按钮,我添加了每个UIBezierPath的几个监听器。每个 UIBezierPath 都与“viewController.swift”中的一个函数匹配,但在执行“self.ChiffreActuel.text = chif”时出现错误,错误是“ fatal error :在解包可选值时意外发现 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

}

}

形状.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()

}
}

}

}

感谢您的帮助。

最佳答案

只需将该函数放入 UIView 的扩展中即可。

extension UIView {
//function
}

关于ios - swift : How to update UILabel from another class X: UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37746874/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com