SwingerDinger
SwingerDinger

Reputation: 276

Cant't show a integer in a label

I'm trying to make a very simple iOS app where an integer is created within a MutableArray. The integer must present itself in an label in a Custom ViewController. Here is my code:

@interface Photo : NSObject

@property (nonatomic) NSString *name;
@property (nonatomic) NSString *filename;
@property (nonatomic) NSString *naam;
@property (nonatomic) int      leeftijd;
@property (nonatomic) NSString *herkomst;
@property (nonatomic) NSString *club;

@end

My Custom TableViewController with an mutableArray.

@interface PhotosTableViewController () {
    NSMutableArray *photos;
}

@end

@implementation PhotosTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"Soccer Players";

    // Create an array
    photos = [[NSMutableArray alloc]init];

    Photo *pic = [[Photo alloc]init];
    pic.name = @"Ronaldo";
    pic.filename = @"ronaldo";
    pic.naam = @"Christano Ronaldo";
    pic.leeftijd = 29;
    pic.herkomst = @"Portugal";
    pic.club = @"Real Madrid";

    [photos addObject:pic];

And at last my Custom ViewController

@interface DisplayViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *currentImage;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *ageLabel;
@property (weak, nonatomic) IBOutlet UILabel *countryLabel;
@property (weak, nonatomic) IBOutlet UILabel *clubLabel;

@end

@implementation DisplayViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed:self.currentPhoto.filename];
    [self.currentImage setImage:image];
    self.nameLabel.text = [self.currentPhoto naam];
    self.ageLabel.text = [self.currentPhoto leeftijd];
    self.countryLabel.text = [self.currentPhoto herkomst];
    self.clubLabel.text = [self.currentPhoto club];
}

Not sure what i'm doing wrong. I gave the integer a Property , gave it a amount en declared it in a label. Must be something very stupid, but can't see the solution on the web.

Thanks for your help!

Upvotes: 0

Views: 62

Answers (3)

Avt
Avt

Reputation: 17043

Integer is not auto converted to NSString. Change

self.ageLabel.text = [self.currentPhoto leeftijd];

to

self.ageLabel.text = [[self.currentPhoto leeftijd] description];

or to

self.ageLabel.text = [NSString stringWithFormat:@"%i", [self.currentPhoto leeftijd]];

Upvotes: 0

Sumit Mundra
Sumit Mundra

Reputation: 3901

Lable only show NSString so you convert your int to NSString..

self.ageLabel.text = [NSString stringWithFormat:@"%d", [self.currentPhoto leeftijd]];

Upvotes: 0

Greg
Greg

Reputation: 25459

You should 'convert' int to string:

self.ageLabel.text = [NSString stringWithFormat:@"%d", [self.currentPhoto leeftijd]];

Upvotes: 2

Related Questions