【问题标题】:covert label text to integer [closed]将标签文本转换为整数 [关闭]
【发布时间】:2012-06-29 12:45:35
【问题描述】:

我创建了一个计数动画,现在我想创建一个包含标签文本值的整数,如下所示:如果我的标签显示数字,那么我的整数将等于 1,依此类推。我做吗? 这是我的代码:

-(void)countup
{
    count = 1;
    MainInt += 1;
    numbersLabel.text = [NSString stringWithFormat:@"%i", MainInt];
    NSLog(@"%d", count);

}

-(void)viewdidload
{
 [numbersLabel setOutlineColor:[UIColor whiteColor]];
[numbersLabel setGradientColor:[UIColor blackColor]];
numbersLabel.drawOutline = YES;
numbersLabel.drawDoubleOutline = YES;
numbersLabel.text = @"start";
//numbersLabel.font = [UIFont fontWithName:@"Ballpark" size:220];
numbersLabel.font = [UIFont fontWithName:@"Arial" size:220];


    MainInt = 1;
    numbersTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(countup) userInfo:nil repeats:YES];
}

谢谢!

【问题讨论】:

  • 还不完全确定您要做什么?您显示的代码已经使用计时器和计数例程进行计数,您还想做什么?

标签: objective-c xcode animation uilabel


【解决方案1】:

如果标签只包含数值(或者如果数值在标签中排在第一位),您可以简单地在字符串上调用intValue

int i = label.text.intValue;

相关文档在这里:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/intValue

【讨论】:

  • 谢谢!我不明白我怎么能忘记这个功能......
  • 没问题,只是不要忘记“接受”答案。 :)
猜你喜欢
  • 2012-12-11
  • 1970-01-01
  • 2018-04-03
  • 2023-04-04
  • 2021-01-24
  • 2020-04-14
  • 2021-11-07
  • 1970-01-01
  • 2011-07-24
相关资源
最近更新 更多