2012-04-24 64 views
1

我正面臨着這個無法解決的問題,並很樂意爲您提供幫助。ZBarReaderViewController,視圖控制器層次結構和其他

我有一個使用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]; 

主視圖控制器中的掃描儀的委託方法:

//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]; 

With:

[self presentModalViewController:resultsViewController animated:YES]; 

但是,如果我這樣做沒有任何反應。

從視圖控制器我這樣做是爲了回到主視圖控制器在結果中:

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

我有一個類似的應用程序,使用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我可以建議你以後就會發布通知[拾取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]; 
} 

希望有所幫助:)

+0

我的委託方法 - (void)imagePickerController:(UIImagePickerController *)picker didFinish PickingMediaWithInfo:(NSDictionary *)info沒有被調用。任何幫助? – Nil 2012-12-29 04:50:16

相關問題