Reputation: 51
I'm trying to create a basic app that takes info that the user puts in to display it on a second view controller.I have a button that push(deprecated)
to ViewControllerTwo with a identifier that is "calButtonPressed
" but then I want it to display float BAC on a label on ViewControllerTwo. The label just says nothing but it does go to the next ViewController.It also has no errors in the code. What can I do to fix this? Thanks :)
Code on ViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"calButtonPressed"])
{
ViewControllerTwo *vc2 = (ViewControllerTwo *)segue.destinationViewController;
vc2.lblBAC.text = [[NSString alloc] initWithFormat:@"%f", BAC];
}
}
Code on ViewController.h
@property (strong, nonatomic) IBOutlet UILabel *lblBAC;
Upvotes: 0
Views: 77
Reputation: 499
In ViewCongtrollerTwo, add a property to hold the text until the view has been loaded.
@interface ViewControllerTwo ()
@property (strong, nonatomic) NSString *textFromlblBAC;
@end
Use this instead in your prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"calButtonPressed"])
{
ViewControllerTwo *vc2 = (ViewControllerTwo *)segue.destinationViewController;
vc2.textFromlblBAC = [[NSString alloc] initWithFormat:@"%f", BAC];
}
And then also in ViewCongtrollerTwo, copy this value into the labels text field only after the text field has been created and the view has been loaded.
- (void)viewDidLoad
{
[super viewDidLoad];
viewControllerTwoLabel.text = self.textFromlblBAC;
}
Upvotes: 2
Reputation: 9352
You can't set lblBAC because vc2's view has not yet loaded. Instead, declare a string property on vc2, set that in prepareForSegue, and then set lblBAC.text to that property in the viewDidLoad of vc2.
Upvotes: 1