Question

I have a simple code to take an integer and pass it to the next view but the int always ends up as 0 on the other view. I know it's not a problem with the variable because it turns out as the real int on the main view. Here is my code:

AnswerViewController *ansViewController = [[AnswerViewController alloc] initWithNibName:@"AnswerViewController" bundle:nil];
ansViewController.num = theNum;
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *mainView = [storyBoard instantiateViewControllerWithIdentifier:@"ansView"];
[self presentViewController:mainView animated:YES completion:nil];
Was it helpful?

Solution

Here is the right way to it:

//Initialize the storyboard
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//initialize the ViewController
AnswerViewController *ansViewController = 
   [storyBoard instantiateViewControllerWithIdentifier:@"AnswerViewController"];
//Assign the value to the controller property
ansViewController.num = theNum;
//Finally present the view Controller
[self presentViewController:ansViewController animated:YES completion:nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top