I had to come up with one tweak for my code and also one alternative when used this solution.
In my case I had a Navigation Controller interrupting in the middle. MainViewController Segue was pointing to that Navigation Controller, then there was another Segue from it pointing to SecondViewController.
So if you'll ever need to get your ViewController from Navigation Controller, you could use such code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// ScoreViewController Storyboard Segue
if ([segue.identifier isEqualToString:@"AwesomeSegue"]) {
// Finding ViewController that is needed in Navigation Controller
UINavigationController *navigationController = segue.destinationViewController;
ScoreViewController *scoreViewController = [[navigationController viewControllers] objectAtIndex:0];
// Bellow implement your delegate or any data you want to pass
}
}
Delegate:
scoreViewController.delegate = self;
Pass data:
scoreViewController.newLabel.text = self.oldLabel.text;
Call method:
[scoreViewController updateLabelText:self.oldLabel.text];
By the way, passing data you should assign it to the IBOutlet
from the Sugue directly.
If you just pass NSString
(or anything else), and will try to assign its text to the UILabel
in viewDidLoad
, it will be Nill
.
It is because viewDidLoad
is implemented before Segue.
Using viewDidAppear
doesn't help here either, as it would load the text after SecondViewController is presented to the screen. You might see how UILabel
text changes from "Label" to "silly name".
Well, if any one has some suggestions, I'm happy to hear, as I'm also just a newbie.