将int值显示为标签

时间:2014-05-06 10:44:33

标签: ios objective-c

晚上好,我是iOS新手并且有一个非常快速的问题。

我需要帮助的是如何获得我的标签"标签"反映" Number"的价值这是一个原始的int我设置为25.问题是,它显示27896而不是显示27896。我确定这是另一种数据格式的25,但任何人都可以帮我用什么代码来显示实际的int价值25?

当用户按下带有ID"执行"

的按钮时,我希望这种情况发生

这是我的代码。谢谢!

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

int Number = @"25";


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)Execute:(id)sender {

    Number = Number * 2;

    [_Label setText:[NSString stringWithFormat:@"%d", Number]];



}
@end

谢谢你们,你的帮助会非常渺茫。

3 个答案:

答案 0 :(得分:2)

首先你写

int Number = @"25";

使用

更改代码
int Number = 25;

如果你想将int显示为字符串,那么你的代码是正确的;

[_Label setText:[NSString stringWithFormat:@"%d", Number]];

答案 1 :(得分:1)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

int number = 25;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)Execute:(id)sender {

    number = number * 2;

    [_Label setText:[NSString stringWithFormat:@"%d", number]];



}
@end

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

NSNumber number = @25;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)Execute:(id)sender {

    number = [NSNumber numberWithInt:[number intValue ] * 2];
// or
    number = @([number intValue ] * 2);

    [_Label setText:[NSString stringWithFormat:@"%@", number]];



}
@end

您所做的是,您创建了一个名为int的{​​{1}}(无论如何都应为Number)并为其指定了一个值{{1}的常量number对象(作为字符串)。 NSString创建一个"25"对象,其值为@25

答案 2 :(得分:0)

阅读此guide并使用属性。祝你好运。