Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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_Uilabel_Swift4_Uicontainerview - Fatal编程技术网

Ios (UILabel)nil,无法在标签中插入文本

Ios (UILabel)nil,无法在标签中插入文本,ios,uilabel,swift4,uicontainerview,Ios,Uilabel,Swift4,Uicontainerview,你好 我似乎有这么一个简单的问题,但我就是想不通 我在视图控制器中有一个容器视图。在那个容器里我几乎没有标签。容器有自己的视图控制器。在容器的视图控制器中,我有一个正在运行的计时器,我希望该标签显示计时器。但每次我使用这个标签,应用程序都会崩溃 “线程1:致命错误:在展开可选值时意外发现nil” 如果我对带有此标签的行进行注释,则一切正常 @IBOutlet weak var timeLabel: UILabel! var counter = 0.0 var timer = Timer

你好

我似乎有这么一个简单的问题,但我就是想不通

我在视图控制器中有一个容器视图。在那个容器里我几乎没有标签。容器有自己的视图控制器。在容器的视图控制器中,我有一个正在运行的计时器,我希望该标签显示计时器。但每次我使用这个标签,应用程序都会崩溃 “线程1:致命错误:在展开可选值时意外发现nil”

如果我对带有此标签的行进行注释,则一切正常

    @IBOutlet weak var timeLabel: UILabel!

var counter = 0.0
var timer = Timer()
var isRunning = false


func startStopTimer () {
    if isRunning {
        timer.invalidate()
        isRunning = false
    }else {
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
        isRunning = true
    }
}

@objc func updateTimer() {
    counter = counter + 0.1

    timeLabel.text = String(counter) 
}
这是我第一次在主情节提要中使用容器视图

谁知道我做错了什么,或者有什么建议我可以尝试改变

谢谢 乔纳斯


完整代码

    class MainViewController: UIViewController {
@IBOutlet weak var topContainer: UIView!
@IBOutlet weak var informationContainer: UIView!

@IBOutlet weak var startStopButtonOutlet: UIButton!

let informationContainerVC = InformationContainerViewController()

override func viewDidLoad() {
    super.viewDidLoad()

    setupView()
}

func setupView() {
    topContainer.layer.cornerRadius = 5
    topContainer.layer.masksToBounds = true

    informationContainer.layer.cornerRadius = 5
    informationContainer.layer.masksToBounds = true

    startStopButtonOutlet.layer.cornerRadius = 5
    startStopButtonOutlet.layer.masksToBounds = true
}

@IBAction func startStopButton_TouchUpInside(_ sender: UIButton) {
    informationContainerVC.startStopTimer()

    UIButton.animate(withDuration: 0.1, delay: 0.0, options: [.allowUserInteraction, .curveEaseOut], animations: {
        sender.transform = CGAffineTransform.identity

    }, completion: nil)

    if informationContainerVC.isRunning {
        startStopButtonOutlet.setTitle("Push to Pause", for: .normal)
    }else {
        startStopButtonOutlet.setTitle("Push to Start", for: .normal)
    }
}

@IBAction func startStopButton_TouchDown(_ sender: UIButton) {
    UIButton.animate(withDuration: 0.1, delay: 0.0, options: [.allowUserInteraction, .curveEaseIn], animations: {
        sender.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)

        if self.informationContainerVC.isRunning {

            sender.backgroundColor = UIColor.white.withAlphaComponent(0.5)
        }else {
            sender.backgroundColor = UIColor.green.withAlphaComponent(0.8)
        }



    }, completion: nil)
}

@IBAction func startStopButton_TouchUpOutside(_ sender: UIButton) {
    UIButton.animate(withDuration: 0.1, delay: 0.0, options: [.allowUserInteraction, .curveEaseOut], animations: {
        sender.transform = CGAffineTransform.identity

    }, completion: nil)
}
}
以下是容器视图控制器的代码

class InformationContainerViewController: UIViewController {

@IBOutlet weak var timeLabel: UILabel!

var counter = 0.0
var timer = Timer()
var isRunning = false


func startStopTimer () {
    if isRunning {
        timer.invalidate()
        isRunning = false
    }else {
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
        isRunning = true
    }
}

@objc func updateTimer() {
    counter = counter + 0.1

    timeLabel.text = String(counter)

}
}

当您说
let informationContainerVC=InformationContainerViewController()
时,您正在创建一个未链接到情节提要的
InformationContainerViewController
的新实例,因此没有设置任何出口

您需要获取对容器视图中实际存在的视图控制器实例的引用。您可以在
prepare(for segue:)
中执行此操作;如果您查看故事板,您将看到有一个嵌入序列将包含的视图控制器链接到包含的视图控制器

在MainViewController中:

var informationContainerVC: InformationContainerViewController?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let destVC = segue.destination as? InformationContainerViewController {
        self.informationContainerVC = destVC
    }
}

@IBAction func startStopButton_TouchUpInside(_ sender: UIButton) {
    informationContainerVC?.startStopTimer()

    UIButton.animate(withDuration: 0.1, delay: 0.0, options: [.allowUserInteraction, .curveEaseOut], animations: {
        sender.transform = CGAffineTransform.identity

    }, completion: nil)

    if informationContainerVC?.isRunning {
        startStopButtonOutlet.setTitle("Push to Pause", for: .normal)
    } else {
        startStopButtonOutlet.setTitle("Push to Start", for: .normal)
    }
}

现在您将有一个对正确视图控制器实例的引用

检查插座是否连接到情节提要。它连接到了正确的标签。何时调用startStopTimer?从mainViewController上的按钮调用startStopTimer。此容器与按钮位于同一视图控制器上。您可能希望共享两个类的完整代码。