【问题标题】:iOS: Programmatically called segue not workingiOS:以编程方式调用 segue 不起作用
【发布时间】:2015-08-31 04:57:16
【问题描述】:

我正在使用通过方法调用但没有被触发的 SWRevealViewController segue。不会向日志报告错误,也不会导致应用程序挂起或崩溃。我检查了故事板上的 segue 链接,它具有正确的类和标识符。

- (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];
}

【问题讨论】:

  • 为什么所有的[super viewDidLoad] 电话?那只能从viewDidLoad 内部调用?我认为您还需要显示更多代码。即什么叫validateBarcodeFound,什么时候叫?我可能会将您的请求代码移至 viewWillAppear,以便您知道您的 UI 已完全设置。
  • 确保你在主队列中执行你的 segue。我看到你的异步请求回调是在一个单独的 NSOperationQueue 上执行的,所以你需要 dispatch_asyncdispatch_get_main_queue 执行 segue 的代码。
  • @Sega-Zero 是对的。非常感谢!
  • 做了一个回答,你可以接受它:)

标签: ios objective-c swrevealviewcontroller


【解决方案1】:

始终在主队列中执行转场。您的回调在单独的NSOperationQueue 中执行,您需要将performSegueWithIdentifier 包装在dispatch_async(dispatch_get_main_queue,... 中。

另外,正如 cmets 中提到的 @rory-mckinnel,删除不必要的 [super viewDidLoad] 调用可能会导致意外结果。

【讨论】:

  • 拯救了我的一天 :)
【解决方案2】:

在我的 Swift 案例中,我使用的是:

shouldPerformSegue(withIdentifier: String, sender: Any)

代替:

performSegue(withIdentifier: String, sender: Any)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 2013-12-31
    • 2017-10-22
    • 2014-01-01
    • 1970-01-01
    相关资源
    最近更新 更多