【问题标题】:If Else not allowing color change?If Else 不允许颜色变化?
【发布时间】:2015-12-18 03:12:07
【问题描述】:

我想要 3 个 If/Else If 语句,根据收到的值改变背景颜色。到目前为止,我的代码是:

/////Activity Response Selection/////
@IBAction func ActivityAction(sender: AnyObject) {

    if(ActivitySeg.selectedSegmentIndex == 0)
    {
    ActivityVal.text = "Absent"
    ActivityNum.text = "0"
    }
    else if(ActivitySeg.selectedSegmentIndex == 1)
    {
    ActivityVal.text = "Arms/Legs Flexed"
    ActivityNum.text = "1"
    }
    else if(ActivitySeg.selectedSegmentIndex == 2)
    {
    ActivityVal.text = "Active Movement"
    ActivityNum.text = "2"
    }


let ActivityIn = Double(ActivityNum.text!)
let PulseIn = Double(PulseNum.text!)
let GrimIn = Double(GrimNum.text!)
let AppIn = Double(AppNum.text!)
let RespIn = Double(RespNum.text!)

let product = ActivityIn! + PulseIn! + GrimIn! + AppIn! + RespIn!

TotalVal.text = "\(product)"
TotalVal.text = NSString(format: "APGAR Score: %2.0f", product)as String


if product >= 7.0 {
    TotalVal.backgroundColor = UIColor.greenColor()
}
else if product <= 6.0{
    TotalVal.backgroundColor = UIColor.yellowColor()
}
else if product <= 3.0 {
    TotalVal.backgroundColor = UIColor.redColor()
}

}

我有任何大于/等于 7 的免费值。 等于/小于 6 是黄色背景。 等于/小于 3 是红色背景。

我的问题在于应用程序只会使用绿色和黄色,而完全忽略了红色。如何激活红色?

【问题讨论】:

    标签: ios iphone swift if-statement uilabel


    【解决方案1】:

    问题是你最后的 else if 语句。

    您的红色 (

    要更正它,您可以简单地交换两个 else if 括号。先检查红色,然后检查黄色。

    或为了保持一致性,将其更改为:

    if product >= 7.0 {
        TotalVal.backgroundColor = UIColor.greenColor()
    } else if product > 3.0 {
        TotalVal.backgroundColor = UIColor.yellowColor()
    } else {
        TotalVal.backgroundColor = UIColor.redColor()
    }
    

    检查相同的不等号以避免此类错误总是一个好主意。

    【讨论】:

    • 这是有道理的。谢谢!
    猜你喜欢
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多