Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UILabel意外返回nil_Ios_Swift_Uilabel - Fatal编程技术网

Ios UILabel意外返回nil

Ios UILabel意外返回nil,ios,swift,uilabel,Ios,Swift,Uilabel,制作基本的秒表。我的模型正在更新控制器,但当我尝试更新我的UILabel时,它返回nil 当我试图从计时器更新时,它只返回nil。如果我不使用计时器更新控制器,它将更新标签 我想我一定错过了什么 视图控制器: class ViewController: UIViewController { var stopWatchBrainInstance = stopWatchBrain() //Outlets @IBOutlet weak var timerLabel: UILab

制作基本的秒表。我的模型正在更新控制器,但当我尝试更新我的
UILabel
时,它返回
nil

当我试图从计时器更新时,它只返回
nil
。如果我不使用计时器更新控制器,它将更新标签

我想我一定错过了什么

视图控制器:

class ViewController: UIViewController {
    var stopWatchBrainInstance = stopWatchBrain()
    //Outlets
    @IBOutlet weak var timerLabel: UILabel!

    var displayValue:Int{
        get{
            print("getting Display Val")
            return Int(timerLabel.text!)!
        }
        set{
            timerLabel?.text! = String(val)
        }
    }

    @IBAction func buttonPressed(_ sender: UIButton) {
        if let buttonId = sender.currentTitle{
            let testVAr = displayValue

            stopWatchBrainInstance.setButton(pressed: buttonId, currentDisplayVal: testVAr)
            displayValue = stopWatchBrainInstance.timerVal
        }
    }
}
型号:

class stopWatchBrain:NSObject {
    //Public API

    public  func setButton(pressed identifier:String,currentDisplayVal:Int){
        switch identifier{
        case "Start":
            startTimer(currentTime: currentDisplayVal)
            break
        case "Stop":
           stopTimer()
        default:
            break
        }
    }

    public var timerVal: Int {
        get{
            return stopwatchValue
        }
    }

    //Vars
    private var timer = Timer()
    private var stopwatchValue:Int = 1

    //Methods
    private  func startTimer (currentTime: Int){
        timer = Timer.scheduledTimer(timeInterval: 1, target: self,   selector: (#selector(self.runtheTimer)) , userInfo: nil, repeats: true)
    }

    private  func stopTimer (){
        print("stopping timer")
        timer.invalidate()
    }

    @objc private func runtheTimer(){
        stopwatchValue += 1
        print(stopwatchValue)

        ViewController().setTitle(stopwatchValue)
    }
}

问题在于这一行:

ViewController().setTitle(stopwatchValue)

你知道它是干什么的吗?它创建了一个全新的、独立的ViewController对象(不是界面中的对象),调用其
setTitle
方法并将其丢弃。

Matt给了您代码的主要缺陷,尽管还有一些其他问题需要清理

至于您的
StopWatchBrain
将如何调用视图控制器,这将是委托设计模式的一个很好的用例。请仔细阅读。你应该给你的
StopWatchBrain
类一个委托,当值改变时它会调用这个委托


您会使视图控制器成为
StopWatchBrain
的委托,当使用新的计时器值调用其委托方法时,它会更新其标签。

与您的问题无关,但您的
displayValue
属性实现不佳。不要将标签用作数据。只需使用标签来显示值。将属性更改为:
var displayValue=0{didSet{timerLabel=String(displayValue)}
。注意,谢谢maddy@rmaddy你的意思是
timerLabel.text=String(displayValue)
@SusanHo不要强制打开Int初始值设定项。文本输入可能不是有效的整数。如果失败,请使用nil合并运算符返回默认值<代码>返回Int(timerLabel.text!)??0@LeoDabus是的,谢谢。我们需要一个好的代码完成/编译器插件来实现SO.ahhh。那么,我该如何调用我想要的视图控制器?@SusanHo您需要在创建视图控制器时为StopWatchBrain提供一个(弱)引用,以便它能够找到视图控制器。