1

私はこの解決できない問題に直面しており、喜んでお手伝いさせていただきます。

ZBar バーコード スキャナーを使用する iPhone アプリがあります。UIButton を押すと ZBar スキャナを呼び出すメイン ビュー コントローラがあります。スキャナーが起動し、バーコード番号が検出されると、それ自体を終了し、スキャンの結果を表示する結果ビュー コントローラーを呼び出します。私の問題は、結果のviewcontrollerを閉じることです-何らかの理由でそれを消すことができず、きれいな方法でメインのview controllerに戻ります。私の回避策は、メイン ビュー コントローラーの新しいオブジェクトを作成して呼び出すことでした。これは本当に悪い設計です。

これが私のコードです - 助けていただければ幸いです!

メイン ビュー コントローラーのどこかでスキャナーを呼び出す (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 内から、これを実行してメインの View Controller に戻ります。

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

お待ちいただきありがとうございます。

4

1 に答える 1

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

application: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];
}

お役に立てば幸いです:)

于 2012-04-24T06:22:55.253 に答える