2

When working with storoboard through code we can instantiate controllers via identifier, but if the controller with this identifier is not found will be crash. How I can avoid crashes and/or check existence of the controller in advance?

Controller instantiate example:

UIViewController *controller = [[UIStoryboard storyboardWithName:@"Settings" bundle:nil] instantiateViewControllerWithIdentifier:model.identifier];
[self.navigationController pushViewController:controller animated:YES];
d0ping
  • 462
  • 4
  • 16
  • Why would you want to prevent the crash? It will only happen if you forget to add the identifier, so a crash will alert you to the fact that you need to add it. Once you fix it, it will never happen again. – rdelmar Feb 02 '15 at 07:07
  • Yes, it so, but I get identifier list and I can't guarantee the existence of an appropriate controller in the storyboard. When identifier is not found I should ignore this. – d0ping Feb 02 '15 at 07:25
  • Why can't you guarantee that the Controller exits in your list? – Popeye Feb 02 '15 at 08:27
  • Because settings list getting from the server. In the mobile app part of settings might be not implement. – d0ping Feb 02 '15 at 08:33
  • I think you should be more worried then that you can't guarantee this will get into the app store. Apple don't like getting tricked (That's how they would see this), all your controllers should be included and made reachable from within the App. The reason behind this is because developers could develop apps that look good when in the review process then they flip a switch and all of a sudden it turns into an app that displays pornographic images or encourages the use of drugs and drink. – Popeye Feb 02 '15 at 08:38
  • Also in my opinion it would be a bad user experience if your app realize on a server to tell it which page to load. What if the server went down wouldn't this make your app unusable? – Popeye Feb 02 '15 at 08:40
  • Maybe I'm wrong explained, but the server only returns a list of strings (identifier list). Appropriate controllers should be included in the App but in case required controller was not found crash should not occur. – d0ping Feb 02 '15 at 09:19

2 Answers2

0

Try this. Might not be exactly what you are looking for . Its a work around.

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad 
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
}

- (IBAction)go:(id)sender 
{
    SecondViewController *second;

    @try {
        second = [_story instantiateViewControllerWithIdentifier:@"Hello"];
    }
    @catch (NSException *exception) {
        NSLog(@"Reason %@" , exception.reason);
    }
    @finally {
        if (second == nil) {
            NSLog(@"VC %@" , second);
        }
    }


}
@end
  • Yes, the use of @try-@catch helped to solve problem. Thanks for you answer. – d0ping Feb 02 '15 at 08:27
  • 1
    Why would you do the first `NSLog` when it is guaranteed to be `nil`? Also class names should start with uppercase values so it should be `SecondViewControllerViewController` – Popeye Feb 02 '15 at 08:28
  • I forgot to remove that NSLog statement . Thanks for the tip on class names. – Abhishek Nath Feb 02 '15 at 08:39
  • 2
    Exception handling is unsupported in Cocoa. This solution should only be used for debugging but can't be used in production. – Nikolai Ruhe Feb 02 '15 at 08:49
  • Can you please elaborate or provide a source with detailed information on this? @Nikolai – Abhishek Nath Feb 02 '15 at 08:54
  • There's lots of information on this on [stackoverflow](http://stackoverflow.com/q/4648952/104790) or from [Apple](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Exceptions/Articles/ExceptionsAndCocoaFrameworks.html#//apple_ref/doc/uid/TP40009045-SW1) – Nikolai Ruhe Feb 02 '15 at 09:19
  • @NikolaiRuhe, both of your links just say that a programmer should use exception handling very carefully. And of course exception handling is supported in Cocoa. – vahotm Nov 21 '17 at 14:15
  • @vahotm No. Apple clearly warns you to use exception handling: „The Cocoa frameworks are generally not exception-safe. The general pattern is that exceptions are reserved for programmer error only, and the program catching such an exception should quit soon afterwards.“ – Nikolai Ruhe Nov 22 '17 at 11:18
-3

I have a useful extension for such a case. Check GitHub: https://github.com/orkenstein/AAStoryboardInstantiate

orkenstein
  • 2,810
  • 3
  • 24
  • 45