Is it possible to check whether an identifier exists in a storyboard before instantiating the object?

后端 未结 6 1392
-上瘾入骨i
-上瘾入骨i 2021-02-19 06:53

In my code I have this line, but I was wondering if there is way to check whether @\"SomeController\" exists before I use it with the \"instantiateViewControllerWit

6条回答
  •  孤街浪徒
    2021-02-19 07:27

    As Tom said, the best solution to this problem is the try-catch block:

    @try {
            UIViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"identifier"];
    
        }
        @catch (NSException *exception) {
            UIAlertView *catchView;
    
            catchView = [[UIAlertView alloc]
                         initWithTitle: NSLocalizedString(@"Error", @"Error")
                         message: NSLocalizedString(@"Identifier not found on SB".", @"Error")
                         delegate: self
                         cancelButtonTitle: NSLocalizedString(@"OK", @"Error") otherButtonTitles: nil];
    
            [catchView show];
        }
    

    I hope it helps! even though the answer is really late.

提交回复
热议问题