0

I have the following code:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyViewController" bundle:nil];
UINavigationController *navControllerInstance = [storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([MyViewController class])];

When I use targets my problem is [MyViewController class] is returning "MyTarget_2.MyViewController" then my app crash because "'Storyboard (<UIStoryboard: 0x600001e65d80>) doesn't contain a view controller with identifier "

I have two targets: MyTarget and MyTarget_2 and is only failing with the last one.

I don't understand is why [MyViewController class] is returning MyTarget.MyViewController. When I start a new project this is not happening, only returns MyViewController . Why is such a difference?

This seems to be related because I'm using swift+objective-c Calling NSStringFromClass on a Swift Class in Objective-C returns module mangled name

Ricardo
  • 7,921
  • 14
  • 64
  • 111
  • 1
    Simple solution will be using string @"MyViewController" or defining some constant for that string and using it instead of deriving it from `NSStringFromClass...`. Is there a reason you are avoiding it? – Vivek Molkar Aug 31 '17 at 06:12
  • What do you mean bei "When I use targets ..."? And can you complete the error message? "... doesn't contain a view controller with identifier ..."? – André Slotta Aug 31 '17 at 06:14
  • @AndréSlotta I mean, when i compile with MyTarget is everything ok, but when I compile with MyTarget_2 then I got crash. – Ricardo Aug 31 '17 at 06:17
  • @VivekMolkar you are right but I'm looking for an elegant solution, I think this could became a mayor problem. – Ricardo Aug 31 '17 at 06:18
  • Are you sure that **all** storyboards and classes are included in **all** targets? And what is the *identifier* in the error message? You did not paste it completely... – André Slotta Aug 31 '17 at 06:18
  • @AndréSlotta yes they are included in all targets. – Ricardo Aug 31 '17 at 06:20
  • Can you share your project? – André Slotta Aug 31 '17 at 06:28
  • @AndréSlotta nope :/ – Ricardo Aug 31 '17 at 06:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153315/discussion-between-andre-slotta-and-ricardo). – André Slotta Aug 31 '17 at 06:30

3 Answers3

0

You can get class name like this :

NSString *className = [[NSStringFromClass([MyViewController class]) componentsSeparatedByString:@"."] lastObject];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyViewController" bundle:nil];
UINavigationController *navControllerInstance = [storyboard instantiateViewControllerWithIdentifier:className];

and make sure that your storyboard id should be MyViewController

Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32
  • I'm getting the same error `'NSInvalidArgumentException', reason: 'Storyboard () doesn't contain a view controller with identifier 'MyViewController''` – Ricardo Aug 31 '17 at 06:27
0

Did you give Identifier to your ViewController in storyboard ?

You can give it against Storyboard ID in ViewController's identify Inspector section.

Because ,

UINavigationController *navControllerInstance = [storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([MyViewController class])];

method work on the identifier and not on class name

Tej
  • 71
  • 1
  • 4
0

I think you haven't copied that particular storyboard in to your target. You can verify it. Click on target. Then on Build Phase -> Copy bundle resource. Check if storyboard file exist in both targets. And if storyboard is not there click on + button add that file. Clean and run. Hope it help.

B25Dec
  • 2,301
  • 5
  • 31
  • 54