华工小软 2018-11-20 11:55 采纳率: 100%
浏览 982
已采纳

XCode如何使得Button上的label可以动态显示数字?

本人刚刚开始学习ios开发,老师要求实现一个刷牙APP的UI界面,遇上了几个问题,卡了两天了。。
我制作了一个UI界面 如图图片说明 主要遇上两个问题:
①我希望首次加载粉色女孩界面时,星星上的数字能显示daysStar数组里的Int数字。但在viewDidLoad()中写的button.titleLabel?.text=String(daysStar[1])语句并不能实现这个功能,请问原因是什么呢?(daysStar已经初始化为1,但真机运行时所有数字都初始显示0)
②我在storyboard中给三行按钮的titlelabel都写了一个0,每次点击右上角的刷牙按钮时,星星按钮的label数字会递增,但点击一下星星按钮,数字就会显示0。我尝试过不写0,但是不写0的话,点击按钮不会出现数字,也尝试过把星星用View叠加到粉色矩形上,但按钮的可用性会差很多。这个问题怎么解决呢?(星星按钮是使用了粉色为底的五角星图片)
代码有什么写的不好的地方,也欢迎大家指出呀!
MM界面代码如下:

import UIKit

class mmViewController: UIViewController {

var firstWeekday:Int?;
var daysStar = [Int](repeating: 1, count: 21)


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    // Initialize weekday labels
    // print(daysStar)
    // var daysStar = [Int](repeating: 1, count: 21)

            // Load the weekdays' label
    switch firstWeekday {
        case 1:
            firstLabel.text="周一"
            secondLabel.text="周二"
            thirdLabel.text="周三"
            fourthLabel.text="周四"
            fifthLabel.text="周五"
            sixthLabel.text="周六"
            seventhLabel.text="周日"
        case 2:
            firstLabel.text="周二"
            secondLabel.text="周三"
            thirdLabel.text="周四"
            fourthLabel.text="周五"
            fifthLabel.text="周六"
            sixthLabel.text="周日"
            seventhLabel.text="周一"
        case 3:
            firstLabel.text="周三"
            secondLabel.text="周四"
            thirdLabel.text="周五"
            fourthLabel.text="周六"
            fifthLabel.text="周日"
            sixthLabel.text="周一"
            seventhLabel.text="周二"
        case 4:
            firstLabel.text="周四"
            secondLabel.text="周五"
            thirdLabel.text="周六"
            fourthLabel.text="周日"
            fifthLabel.text="周一"
            sixthLabel.text="周二"
            seventhLabel.text="周三"
        case 5:
            firstLabel.text="周五"
            secondLabel.text="周六"
            thirdLabel.text="周日"
            fourthLabel.text="周一"
            fifthLabel.text="周二"
            sixthLabel.text="周三"
            seventhLabel.text="周四"
        case 6:
            firstLabel.text="周六"
            secondLabel.text="周日"
            thirdLabel.text="周一"
            fourthLabel.text="周二"
            fifthLabel.text="周三"
            sixthLabel.text="周四"
            seventhLabel.text="周五"
        case 7:
            firstLabel.text="周日"
            secondLabel.text="周一"
            thirdLabel.text="周二"
            fourthLabel.text="周三"
            fifthLabel.text="周四"
            sixthLabel.text="周五"
            seventhLabel.text="周六"
        default :
            firstLabel.text="周一"
            secondLabel.text="周二"
            thirdLabel.text="周三"
            fourthLabel.text="周四"
            fifthLabel.text="周五"
            sixthLabel.text="周六"
            seventhLabel.text="周日"
    }

    // Initialize each day's stars
    _3but.titleLabel?.text=String(daysStar[0])
    _12but.titleLabel?.text=String(daysStar[1])
    _13but.titleLabel?.text=String(daysStar[2])
    _14but.titleLabel?.text=String(daysStar[3])
    _15but.titleLabel?.text=String(daysStar[4])
    _16but.titleLabel?.text=String(daysStar[5])
    _17but.titleLabel?.text=String(daysStar[6])
    _21but.titleLabel?.text=String(daysStar[7])
    _22but.titleLabel?.text=String(daysStar[8])
    _23but.titleLabel?.text=String(daysStar[9])
    _24but.titleLabel?.text=String(daysStar[10])
    _25but.titleLabel?.text=String(daysStar[11])
    _26but.titleLabel?.text=String(daysStar[12])
    _27but.titleLabel?.text=String(daysStar[13])
    _31but.titleLabel?.text=String(daysStar[14])
    _32but.titleLabel?.text=String(daysStar[15])
    _33but.titleLabel?.text=String(daysStar[16])
    _34but.titleLabel?.text=String(daysStar[17])
    _35but.titleLabel?.text=String(daysStar[18])
    _36but.titleLabel?.text=String(daysStar[19])
    _37but.titleLabel?.text=String(daysStar[20])

}


@IBOutlet weak var firstLabel: UILabel!
@IBOutlet weak var secondLabel: UILabel!
@IBOutlet weak var thirdLabel: UILabel!
@IBOutlet weak var fourthLabel: UILabel!
@IBOutlet weak var fifthLabel: UILabel!
@IBOutlet weak var sixthLabel: UILabel!
@IBOutlet weak var seventhLabel: UILabel!
@IBAction func brush_but(_ sender: Any) {
    if (getCurrentHour()>=5&&getCurrentHour()<=11)||(getCurrentHour()>=19&&getCurrentHour()<=23) {
        daysStar[0]+=4
    }
    else{
        daysStar[0]+=1
    }
    _3but.titleLabel?.text=String(daysStar[0])
}
@IBOutlet weak var _3but: UIButton!
@IBAction func _3but(_ sender: Any) {
}

@IBOutlet weak var _12but: UIButton!
@IBAction func _12but(_ sender: Any) {
}
@IBOutlet weak var _13but: UIButton!
@IBAction func _13but(_ sender: Any) {
}
@IBOutlet weak var _14but: UIButton!
@IBAction func _14but(_ sender: Any) {
}
@IBOutlet weak var _15but: UIButton!
@IBAction func _15but(_ sender: Any) {
}
@IBOutlet weak var _16but: UIButton!
@IBAction func _16but(_ sender: Any) {
}
@IBOutlet weak var _17but: UIButton!
@IBAction func _17but(_ sender: Any) {
}
@IBOutlet weak var _21but: UIButton!
@IBAction func _21but(_ sender: Any) {
}
@IBOutlet weak var _22but: UIButton!
@IBAction func _22but(_ sender: Any) {
}
@IBOutlet weak var _23but: UIButton!
@IBAction func _23but(_ sender: Any) {
}
@IBOutlet weak var _24but: UIButton!
@IBAction func _24but(_ sender: Any) {
}
@IBOutlet weak var _25but: UIButton!
@IBAction func _25but(_ sender: Any) {
}
@IBOutlet weak var _26but: UIButton!
@IBAction func _26but(_ sender: Any) {
}
@IBOutlet weak var _27but: UIButton!
@IBAction func _27but(_ sender: Any) {
}
@IBOutlet weak var _31but: UIButton!
@IBAction func _31but(_ sender: Any) {
}
@IBOutlet weak var _32but: UIButton!
@IBAction func _32but(_ sender: Any) {
}
@IBOutlet weak var _33but: UIButton!
@IBAction func _33but(_ sender: Any) {
}
@IBOutlet weak var _34but: UIButton!
@IBAction func _34but(_ sender: Any) {
}
@IBOutlet weak var _35but: UIButton!
@IBAction func _35but(_ sender: Any) {
}
@IBOutlet weak var _36but: UIButton!
@IBAction func _36but(_ sender: Any) {
}
@IBOutlet weak var _37but: UIButton!
@IBAction func _37but(_ sender: Any) {
}

func getCurrentHour() -> Int {

    var timers: [Int] = []

    let calendar: Calendar = Calendar(identifier: .gregorian)
    var comps: DateComponents = DateComponents()
    comps = calendar.dateComponents([.year,.month,.day, .weekday, .hour, .minute,.second], from: Date())
    timers.append(comps.hour!)

    return timers[0];
}
/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
}
*/

}

  • 写回答

2条回答 默认 最新

  • devmiao 2018-11-20 14:59
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。