iOS:以编程方式调用的 segue 不起作用
iOS: Programmatically called segue not working
我正在使用通过方法调用但未被触发的 SWRevealViewController segue。不会向日志报告任何错误,也不会导致应用程序挂起或崩溃。我已经检查了故事板上的 segue link,它具有正确的 class 和标识符。
- (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];
}
始终在主队列中执行 segue。您的回调在单独的 NSOperationQueue
中执行,您需要将 performSegueWithIdentifier
包装在 dispatch_async(dispatch_get_main_queue,...
中。
此外,正如@rory-mckinnel 在评论中提到的,删除不必要的 [super viewDidLoad]
调用可能会导致意外结果。
在我 Swift 的案例中,我使用的是:
shouldPerformSegue(withIdentifier: String, sender: Any)
而不是:
performSegue(withIdentifier: String, sender: Any)
我正在使用通过方法调用但未被触发的 SWRevealViewController segue。不会向日志报告任何错误,也不会导致应用程序挂起或崩溃。我已经检查了故事板上的 segue link,它具有正确的 class 和标识符。
- (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];
}
始终在主队列中执行 segue。您的回调在单独的 NSOperationQueue
中执行,您需要将 performSegueWithIdentifier
包装在 dispatch_async(dispatch_get_main_queue,...
中。
此外,正如@rory-mckinnel 在评论中提到的,删除不必要的 [super viewDidLoad]
调用可能会导致意外结果。
在我 Swift 的案例中,我使用的是:
shouldPerformSegue(withIdentifier: String, sender: Any)
而不是:
performSegue(withIdentifier: String, sender: Any)