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 Build a Simple iPhone App (iOS7) Refactoring into a Model Readonly Attribute

Mrunal Kharod
Mrunal Kharod
10,657 Points

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.

Jhoan Arango
Jhoan Arango
14,575 Points

Hello:

Please provide your code, this way we can look to see if there is something in your code causing it.

2 Answers

Mrunal Kharod
Mrunal Kharod
10,657 Points

Thanks for the response.....following 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

Mrunal Kharod
Mrunal Kharod
10,657 Points

Sorry about the messy code format. Here is my code (hopefully readable this time)......

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

I am still not being able to see any labels. Anyone is facing the same issue? any solution to that?