Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.

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?