我需要在一个类中更改另一个类中的整数。但是,每次尝试这个时,它都会瞬间改变变量,这很奇怪。我已在此测试应用程序中复制了该问题。
import UIKit
class ViewController: UIViewController {
var theapples = 0
override func viewDidLoad() {
super.viewDidLoad()
let battles = someclass(frame: CGRectMake(0, 0, 90, 90))
self.view.addSubview(battles)
let thetimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("apples"), userInfo: nil, repeats: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func apples(){
++theapples
println("\(theapples)")
}
func santa(){
theapples = 2
println(" this is the number \(theapples)")
}
}
并且是我的一些类代码
import UIKit
class someclass: UIView {
init(frame: CGRect) {
super.init(frame:frame)
self.backgroundColor = UIColor.blackColor()
}
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
let thepear = ViewController()
thepear.santa()
self.removeFromSuperview()
}
}
很抱歉用代码来压倒,但是应该做的是每一秒它应该记录一个比最后一个更高的数字。当我点击我的自定义视图时,它应该将整数theapples设置为2但是它只会暂时执行此操作。这就是日志的样子:
答案 0 :(得分:0)
let thepear = ViewController()
每次调用时都会创建一个新的(=不同的)ViewController实例