Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

In Crystal Ball app Labels disappear when button is pressed....

When I am running the code, it's not giving me any error but when I am pressing the button, I am having blank labels, though my code is exactly the same. Has anyone came across the same issue? does anyone know the solution to this? Responses and suggestions will be appreciated....Thanks. Here is my code.......

ViewController.h :-

import <UIKit/UIKit.h>

@class CrystalBallModel; @interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *predictionLabel; @property (strong,nonatomic) CrystalBallModel *crystalBall;

-(IBAction)buttonPressed;

@end

ViewController.m :-

import "ViewController.h" import "CrystalBallModel.h"

@interface ViewController ()

@end

@implementation ViewController

-(void)viewDidLoad { [super viewDidLoad]; self.crystalBall = [[CrystalBallModel alloc]init]; } -(void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } -(IBAction)buttonPressed { self.predictionLabel.text = [self.crystalBall randomPrediction]; } @end

CrystalBallModel.h :-

import <Foundation/Foundation.h> @interface CrystalBallModel : NSObject { NSArray *_predictions; }

@property (strong,nonatomic,readonly) NSArray *predictions;

-(NSString*) randomPrediction;

@end

CrystalBallModel.m :-

import "CrystalBallModel.h"

@implementation CrystalBallModel

-(NSArray*) _predictions{

if (_predictions == nil) {

_predictions = [[NSArray alloc] initWithObjects:@"it is decidedly so", @"it is certain", @"The stars are not aligned", @"It is doubtful", @"Better not tell you now", @"Concentrate and ask again", nil]; } return _predictions; }

-(NSString*) randomPrediction{

int random = arc4random_uniform((uint32_t)self.predictions.count);

return [self.predictions objectAtIndex:random]; } @end

Anyone is facing the same issue? any solution to that?

2 Answers

I don't know objective-c but possibly when you set the text of the label there is no words so it gives the appearance there is no label?

Thanks for the response, Trent. I have generated an array of strings for the label so that shouldn't be the case......I really appreciate your efforts.