// some VC
Class FirstClass: UIViewController {
var tag = -1 // supposed to represent a tag for a UIView inside of FirstClass
// supposed to change the tag
func abc() {
tag = 5
}
// returns the tag
func getTag() -> Int {
return tag
}
}
// another VC
Class SecondClass: UIViewController {
FirstClass().abc()
var a = FirstClass().getTag() // prints -1; I want it to print 5
let someView = view.viewWithTag(a) as! UIView // nil
}
我想从函数内部更新“tag”变量。为了简单起见,我省略了一些基本函数,例如“viewDidLoad”。 谢谢!
最佳答案
像这样更改 SecondClass 中的代码:
Class SecondClass: UIViewController {
let myfirstClass = FirstClass()
myFirstClass.abc() // changes the tag in your instance of FirstClass
var a = myfirstClass.getTag()
// will print 5 because now you ask the instance,
// where the abc-function was called
}
关于swift - 如何从方法内部更新类变量,以便该变量对 Swift 中的其他类可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57760874/