简体   繁体   English

Swift 2-更改标签颜色只能在模拟器中使用,而不能在iPhone上使用

[英]Swift 2 - Change Label Color Working In Simulator But Not On iPhone

This might be a simple fix but I couldn't find anything about this online. 这可能是一个简单的修复程序,但我在网上找不到任何内容。 I'm coding in Swift 2 and in the Simulator the text color is changing perfectly. 我正在Swift 2中进行编码,而在Simulator中,文本颜色正在完美地变化。 I'm having it change when a certain condition is met. 当满足特定条件时,我会改变它。 When loading it on my phone the color doesn't change at all. 将其加载到我的手机上后,颜色完全不变。 I'm not sure if there is anything else I can do. 我不确定是否还能做其他事情。 I tried adding let before it but it just gave me more errors. 我尝试在它之前添加let ,但它给了我更多错误。

self.TimeCount.textColor = UIColor.redColor();

Rest of the code: 其余代码:

func Countdown(){
        //Time Left Before Bomb Explodes
        TimerCount -= 1
        if(TimerCount <= 0){
            MainTime.font = UIFont(name: NoDiffuseKit.font.fontName, size: 45)
            self.MainTime.textColor = UIColor.redColor();
            MainTime.text = "No Time"
        }else if(TimerCount == 5){
            self.MainTime.textColor = UIColor.redColor();
            MainTime.text = "5"
        }else{
            MainTime.text = "\(TimerCount)"
            Main = TimerCount
        }
        //Time Left To Diffuse With A Kit
        Kit = Main - 5
        if(Kit <= 0){
            DiffuseKit.font = UIFont(name: DiffuseKit.font.fontName, size: 15)
            self.DiffuseKit.textColor = UIColor.redColor();
            DiffuseKit.text = "No Time"
        }else if(Kit == 5){
            self.DiffuseKit.textColor = UIColor.redColor();
            DiffuseKit.text = "5"
        }else{
            DiffuseKit.text = "\(Kit)"
        }
        //Time Left To Diffuse Without A Kit
        NoKit = Main - 10
        if(NoKit <= 0){
            NoDiffuseKit.font = UIFont(name: NoDiffuseKit.font.fontName, size: 15)
            self.NoDiffuseKit.textColor = UIColor.redColor();
            NoDiffuseKit.text = "No Time"
        }else if(NoKit == 5){
            self.NoDiffuseKit.textColor = UIColor.redColor();
            NoDiffuseKit.text = "5"
        }else{
            NoDiffuseKit.text = "\(NoKit)"
        }
    }

    @IBAction func ToggleButton(sender: AnyObject) {
        //If The Button Is At Its Default at 45 Seconds & Not Running
        if(Toggle == false){
            if TimerRunning == false{
                Timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Countdown"), userInfo: nil, repeats: true)
                TimerRunning = true;
                Toggle = true;
            }
        //If Timer is Running & Needs To Be Stopped
        }else if (Toggle == true){
            if TimerRunning == true{
                Timer.invalidate()
                TimerRunning = false
                TimerCount = 45;
                MainTime.text = "45"
                DiffuseKit.text = "40"
                NoDiffuseKit.text = "35"
                Toggle = false;
            }
        }
    }

Look to see if the timer callback is being run in the main thread; 查看是否在主线程中运行了计时器回调; if not use dispatch_asynch(dispatch_get_main_queue(), <YOUR CODE IN BLOCK>) 如果不使用dispatch_asynch(dispatch_get_main_queue(), <YOUR CODE IN BLOCK>)

The issue is that anything that updates the UI must be run from the main thread. 问题在于,任何更新UI的操作都必须从主线程运行。

Also check to make sure the font name you have is correct; 还要检查以确保您使用的字体名称正确。 the device is case sensitive but the default Mac (thus simulator) file system is not. 设备区分大小写,但默认的Mac(因此模拟器)文件系统不区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM