如何添加“1”到 XCode 中的 UILabel

发布于 2024-12-03 17:54:14 字数 100 浏览 5 评论 0原文

我需要将值“1”添加到 UILabel 视图中已有的任何值上。例如,如果 UILabel 的值为 2,我需要加一使其变为 3。我该怎么做?

谢谢,

詹姆斯

I need to add the value "1" onto whatever value is already in a UILabel view. For example, if the value of the UILabel were 2, I need to plus one to make it 3. How can I do this?

Thanks,

James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

方觉久 2024-12-10 17:54:14

NSString 有一个名为 intValue 的便捷方法,您可以像这样使用它:

label.text = [NSString stringWithFormat:@"%d",[label.text intValue]+1];

对于后续问题,只需使用 if 语句:

if (label.tag == 1) {
    label.text = [NSString stringWithFormat:@"%d",[label.text intValue]+1];
} 

或也许你的意思是

if (something) {
    label.tag = 1;
} 

label.text = [NSString stringWithFormat:@"%d",[label.text intValue]+label.tag];

NSString has a handy method called intValue, which you can use like this:

label.text = [NSString stringWithFormat:@"%d",[label.text intValue]+1];

For your follow-up question, simply use an if statement:

if (label.tag == 1) {
    label.text = [NSString stringWithFormat:@"%d",[label.text intValue]+1];
} 

or maybe you mean

if (something) {
    label.tag = 1;
} 

label.text = [NSString stringWithFormat:@"%d",[label.text intValue]+label.tag];
青朷 2024-12-10 17:54:14

在 Swift 2.0 中,我使用:

let currentLabelNumber: Int? = Int(label.text!)! + 1
      label.text = "\(currentlabelNumber!)"

我尝试将其放在

let currentLabelNumber: Int? = Int(label.text!)! + 1

函数的顶部并重用它,但它不喜欢那样。每次我想增加标签值时都必须使用这两行。

希望这对某人有帮助!

In Swift 2.0 I used:

let currentLabelNumber: Int? = Int(label.text!)! + 1
      label.text = "\(currentlabelNumber!)"

I tried putting

let currentLabelNumber: Int? = Int(label.text!)! + 1

at the top of the function and reusing it but it didn't like that. Had to use both lines each time I wanted to increase the label value.

Hope this helps someone!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文