iOS: Programmatically called segue not working

249 views Asked by At

I am using a SWRevealViewController segue that is called via method but isn't getting triggered. No errors are reported to the log, and it doesn't cause the app to hang or crash. I've checked the segue link on the storyboard and it has the correct class and identifier.

- (void)viewDidLoad {

    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString:loadUrl];
    NSLog(loadUrl);
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
        if([data length] > 0 && error == nil)
            [mWebView loadRequest:request];         
        else if (error != nil) 
            NSLog(@"Error: %", error);}
    ];    
}

- (void)     captureOutput:(AVCaptureOutput *)captureOutput   
  didOutputMetadataObjects:(NSArray *)metadataObjects
            fromConnection:(AVCaptureConnection *)connection
{
     [metadataObjects
     enumerateObjectsUsingBlock:^(AVMetadataObject *obj,
                              NSUInteger idx,
                              BOOL *stop)
     {
         if ([obj isKindOfClass:
              [AVMetadataMachineReadableCodeObject class]])
         {
         // 3
             AVMetadataMachineReadableCodeObject *code =
             (AVMetadataMachineReadableCodeObject*)
             [_previewLayer transformedMetadataObjectForMetadataObject:obj];
         // 4
             Barcode * barcode = [Barcode processMetadataObject:code];

             for(NSString * str in self.allowedBarcodeTypes){
                 if([barcode.getBarcodeType isEqualToString:str]){
                     [self validBarcodeFound:barcode];
                     return;
                 }
             }
         }
     }];
}


- (void) validBarcodeFound:(Barcode *)barcode{ 

    [self stopRunning];
    [self.foundBarcodes addObject:barcode];   

    NSString *input = [barcode getBarcodeData];
    [NSString stringWithFormat:@"%lu",(unsigned long) [self.foundBarcodes count]-1];

    if ([input length] >= 13)
    {
        input = [input substringToIndex:12];
    }

    loadUrl = [[@"http://www.mywebsite.com/" stringByAppendingString:input] stringByAppendingString:@"?utm_source=iphone"];
    [super viewDidLoad];
    [self performSegueWithIdentifier:@"toWebControl" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    [super viewDidLoad];
}
2

There are 2 answers

0
Sega-Zero On BEST ANSWER

Always perform a segue in the main queue. Your callback is executing in the separate NSOperationQueue, you need to wrap performSegueWithIdentifier in dispatch_async(dispatch_get_main_queue,....

Plus, as @rory-mckinnel mentioned in the comments, remove unnecessary [super viewDidLoad] calls as in may lead to unexpected results.

0
Doilio Matsinhe On

In my case on Swift, I was using:

shouldPerformSegue(withIdentifier: String, sender: Any)

Instead of:

performSegue(withIdentifier: String, sender: Any)