0

I'm trying to pass parameter from one view controller to another, but application crashes with exception.

My code is this:

SelectedItemViewController *nextView = [storyboard instantiateViewControllerWithIdentifier:@"SelectedItemViewID"];

nextView.m_selectedItemId = [NSNumber numberWithInt:777];

[self presentViewController:nextView animated:YES completion:NULL];

And I have the following in my stack:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setM_selectedItemId:]: unrecognized selector sent to instance 0x9c765d0'

Sport
  • 8,570
  • 6
  • 46
  • 65
Davit Siradeghyan
  • 6,053
  • 6
  • 24
  • 29
  • Are you sure, your view'controller identifier is `SelectedItemViewID`? – Valentin Shamardin Jan 19 '14 at 10:36
  • yes more than sure :) looks like issue related to navigation controller. My view embedded into navigation controller and this ID is a navigation controller id. I don't know how to pass params if there is a navigation controller also. – Davit Siradeghyan Jan 19 '14 at 11:07

3 Answers3

3

In your storyboard, the view controller with identifier SelectedItemViewID is a navigation controller, not a SelectedItemViewController. So, before you set m_selectedItemId you should access the navigation controllers root view controller (which should be your SelectedItemViewController).

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Wain you are right regarding view controller ID, but I'm passing navigation controller ID because my SelectedItemViewController embedded into navigation controller, and I need to redirect to it, otherwise I'm loosing navigation bar. Any ideas how to make this work? – Davit Siradeghyan Jan 19 '14 at 10:57
  • You need to get the view controller from the nav controller, like `navigationController.viewControllers[0];` – Wain Jan 19 '14 at 12:40
2

I found solution here: instantiateViewControllerWithIdentifier and pass data

What should be done in case if you want to pass parameter to view controller via navigation controller:

UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:@"SelectedItemViewID"];

SelectedItemViewController *nextView = navigationController.viewControllers[0];

nextView.m_selectedItemId = [NSNumber numberWithInt:777];

[self presentViewController:navigationController animated:YES completion:NULL];
Community
  • 1
  • 1
Davit Siradeghyan
  • 6,053
  • 6
  • 24
  • 29
0

When you select the view controller in your storyboard, is the class set to SelectedItemViewController?

Obviously you seem to have a @property ... m_selectedItemId (or at least setter method setM_selectedItemId) on SelectedItemViewController, otherwise your code wouldn't even compile. But it seems that [storyboard instantiateViewControllerWithIdentifier:@"SelectedItemViewID"] doesn't really return the expected SelectedItemViewController. Hence I have the feeling that you forgot to set the correct class name to SelectedItemViewController in your storyboard.

plu
  • 1,321
  • 10
  • 14
  • plu code compiles perfectly, and crash happens when I'm trying to redirect to the new view, property also exists, but I don't have setM_selectedItemId, and looks like it's not mandatory to have that method, as far as I was able to understand from other posts in stackoverflow. – Davit Siradeghyan Jan 19 '14 at 10:52
  • Check out Wain's answer, I think he's right :). So in Code it would look like: UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:@"SelectedItemViewID"]; SelectedItemViewController *nextView = (SelectedItemViewController *)navigationController.rootViewController; nextView.m_selectedItemId = [NSNumber numberWithInt:777]; – plu Jan 19 '14 at 10:55