ZBarReaderViewController,查看控制器层次结构等

时间:2012-04-24 00:30:34

标签: iphone objective-c ios uiviewcontroller barcode-scanner

我正面临着这个无法解决的问题,很乐意为你提供帮助。

我有一个使用ZBar条码扫描器的iPhone应用程序。我有一个主视图控制器,可以在推动UIButton时调用ZBar扫描器。一旦扫描仪启动并检测到条形码编号,它就会自动解除,我会调用结果视图控制器,显示扫描的一些结果。我的问题是解雇结果viewcontroller - 从某种原因我不能使它昏迷并以干净的方式回到主视图控制器。我的工作是创建一个主视图控制器的新对象并调用它,这是一个非常糟糕的设计。

这是我的代码 - 感谢任何帮助!

在主视图控制器(UIButton操作方法)中的某处调用扫描器:

ZBarReaderViewController *reader = [ZBarReaderViewController new];
UINavigationController *navCntrl1 = [[UINavigationController alloc] initWithRootViewController:reader];
reader.readerDelegate = self;
reader.title = @"Scan Barcode";
reader.supportedOrientationsMask = ZBarOrientationMaskAll;
ZBarImageScanner *scanner1 = reader.scanner;
[scanner1 setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0];
[scanner1 setSymbology: ZBAR_QRCODE config: ZBAR_CFG_ENABLE to: 0];
[self presentModalViewController:navCntrl1 animated:YES];

主ViewController中的Scanner Delegate方法:

//ZBarSDK Finish Scanning
- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
{
  id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for(symbol in results)
    break;

// EXAMPLE: do something useful with the barcode data

[self dismissModalViewControllerAnimated: YES];

//Calling the Results view controller
Results *resultsViewController = [[Results alloc] initWithNibName:nil bundle:nil];
resultsViewController.tempBarcode = barcode;

UINavigationController *resultsNavigationController = [[UINavigationController alloc] initWithRootViewController:resultsViewController];
resultsNavigationController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

[[[UIApplication sharedApplication]delegate].window setRootViewController:resultsNavigationController];

 }

这是一个说我试图替换的地方:

[[[UIApplication sharedApplication]delegate].window setRootViewController:resultsNavigationController];

使用:

[self presentModalViewController:resultsViewController animated:YES];

但如果我这样做,就没有任何反应。

在Results ViewController中,我这样做是为了返回主视图控制器:

ViewController *mainViewController = [[ViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
mainNavigationController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;    
[self presentModalViewController:mainNavigationController animated:YES];

而不仅仅是这个,它不起作用:

[self dismissModalViewControllerAnimated:YES];

感谢您的耐心等待!

1 个答案:

答案 0 :(得分:1)

我有一个使用ZBar的类似应用。这是我对你的UIButton方法的类比:

- (void)scanButtonTapped{
    ZBarReaderViewController *reader = [ZBarReaderViewController new];
    reader.readerDelegate = self;
    reader.supportedOrientationsMask = ZBarOrientationMaskAll;

    ZBarImageScanner *scanner = reader.scanner;

    // I need to scan only QR-codes
    [scanner setSymbology:0 config:ZBAR_CFG_ENABLE to:0];
    [scanner setSymbology:ZBAR_QRCODE config:ZBAR_CFG_ENABLE to:1];
    reader.readerView.zoom = 1.0;

    [self presentModalViewController:reader animated:YES];
    [reader release];
}

这是我的imagePickerController:didFinishPickingMediaWithInfo:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;

    for (symbol in results) 
        break;

    // Here I get the QR-code text
    self.qrText = symbol.data;
    NSLog(@"QR-code text = %@",self.qrText);

    // Here I hide scanning View Controller from user
    [picker dismissModalViewControllerAnimated:YES];

    // Here I call QR-code text processing logic
    [self ticketCheckOutLogic];
}

为了调用另一个UIViewController,我可以建议在[picker dismissModalViewControllerAnimated:YES]之后发布通知;到您的AppDelegate,例如:

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:self.barcodeText, @"barcodeText", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"newBarcodeScanned" object:nil userInfo:options];

在应用程序中:didFinishLaunchingWithOptions:你可以这样写:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showResultView:)name:@"newBarcodeScanned" object:nil];

...和你的showResultView:可以是这样的:

- (void)showResultView:(NSNotification *)notification {
     //Calling the Results view controller
     Results *resultsViewController = [[Results alloc] initWithNibName:@"ResultsView" bundle:nil];
     NSDictionary *dict = [notification userInfo];
     resultsViewController.tempBarcode = [dict objectForKey:@"barcodeText"];
     [self presentModalViewController:resultsViewController animated:YES];
}

希望有所帮助:)