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!

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

Ben Junya
Ben Junya
12,365 Points

Crystal Ball text disappears when pressing the button

Hi there!

I'm having a problem when I press the button, The text disappears completely. Can someone give me a hand?

PMViewController.h

import <UIKit/UIKit.h>

@class PMCrystalBall;

@interface PMViewController : UIViewController @property (strong, nonatomic) IBOutlet UILabel *predictionLabel;

@property (strong, nonatomic) PMCrystalBall *crystalBall;

  • (IBAction)buttonPressed;

@end

PMViewController.m

import "PMViewController.h"

import "PMCrystalBall.h"

@interface PMViewController ()

@end

@implementation PMViewController

  • (void)viewDidLoad { [super viewDidLoad];

    self.crystalBall = [[PMCrystalBall alloc] init]; }

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

  • (IBAction)buttonPressed {

    self.predictionLabel.text = [self.crystalBall randomPrediction];

} @end

PMCrystalBall.h

import <Foundation/Foundation.h>

@interface PMCrystalBall : NSObject { NSArray *_predictions;

}

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

-(NSString*) randomPrediction;

@end

PMCrystalBall.m

import "PMCrystalBall.h"

@implementation PMCrystalBall

-(NSArray*) predictions { if (_predictions == nil) { _predictions = [[NSArray alloc] initWithObjects:

                    @"It is certain",
                    @"It is decidedly so",
                    @"All signs say yes",
                    @"The stars are not aligned",
                    @"My reply is no",
                    @"It is doubtful",
                    @"Better not tell you now",
                    @"Concentrate and ask again",
                    @"Unable to answer now", nil];
}
return _predictions;

} -(NSString*) randomPredictiton { int random = arc4random_uniform(self.predictions.count); return [self.predictions objectAtIndex:random]; }

@end

Thanks in advance!

1 Answer

Zach Lorber-Termaat
Zach Lorber-Termaat
7,680 Points

Looks like you just need to force the text to be visible or in objective-c not hidden. Try this

(IBAction)buttonPressed { self.predictionLabel.text = [self.crystalBall randomPrediction]; [predictionLabel setHidden:NO]; } @end

I am fairly new at objective-c so don't be upset if this doesn't work, however I don't think it could cause any harm haha.