Swift:如何从另一个类 X 更新 UILabel:UIButton
Posted
技术标签:
【中文标题】Swift:如何从另一个类 X 更新 UILabel:UIButton【英文标题】:Swift : How to update UILabel from another class X: UIButton 【发布时间】:2016-06-10 11:16:40 【问题描述】:我是 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】:只需将该函数放在UIView
的扩展中即可。
extension UIView
//function
【讨论】:
对不起,我不明白您的回复,我该怎么办? 把你的函数放在大括号里。然后说 label.yourFunction() 我尝试了您的解决方案,但我从不使用它。我上网查了一下,没有成功。你能举个例子吗? 好的。您想将哪个功能应用于您的标签。以上是关于Swift:如何从另一个类 X 更新 UILabel:UIButton的主要内容,如果未能解决你的问题,请参考以下文章
swift - 如何从另一个类设置 UIStepper 值?
从另一个类 Swift 访问 navigationItem.title