2012-08-14 16 views
2

我一直在這個問題上坐了幾個小時。搜索了很多,並找不到一個好的答案。iOS ZXingWidget - 在自己的ViewController.view中使用ZXingWidgetViewController的視圖作爲子視圖

我正在嘗試使用iOS zxing Widget進行QR碼掃描。我有一個ViewController,它在我的UINavigationController中作爲一個Item被推送,或者從另一個ViewController Modally地呈現。這個ViewController有3個不同視圖的SegmentedControl。其中兩個視圖是加載簡單網站的UIWebViews,沒有什麼特別的。

選擇看起來是這樣的:

- (IBAction)segmentedControlValueChanged:(id)sender { 
    NSString *urlString; 
    ZXingWidgetController *widController; 
    QRCodeReader* qrcodeReader; 
    NSSet *readers; 
    switch (segmentedControl.selectedSegmentIndex) { 
     case 0: 
      [self.view bringSubviewToFront:self.productSearchWebView]; 
      urlString = [[SACommunicationService sharedCommunicationService] getURLforKey:kURLTypeProductSearch]; 
      [self.productSearchWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]]; 
      break; 
     case 1: 
      [self.view bringSubviewToFront:self.marketSearchWebView]; 
      urlString = [[SACommunicationService sharedCommunicationService] getURLforKey:kURLTypeMarketSearch]; 
      [self.marketSearchWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]]; 
      break; 
     case 2: 
      widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO]; 
      qrcodeReader = [[QRCodeReader alloc] init]; 
      readers = [[NSSet alloc] initWithObjects:qrcodeReader,nil]; 
      widController.readers = readers; 
      [self.QRCodeScannerView addSubview:widController.view]; 
      [self.view bringSubviewToFront:self.QRCodeScannerView]; 
      break; 
     default: 
      break; 
    } 
} 

我試圖調試和走一步看一步,發現問題出在哪裏來自:

解碼器(這是基本的斑馬線邏輯的一部分)嘗試從它的代理(應該是ZXingWidgetController類)調用「failedToDecodeImage:」並崩潰(EXC_BAD_ACCESS)

在逐步調試過程中,我發現調用了ZXingWidgetController的「取消」方法。現在我不知道爲什麼這個方法被調用。在初始化並啓動解碼器後,Widget不應該停止。

我希望你們能幫忙!

回答

0

你不應該添加控制器視圖作爲另一個視圖的子視圖。你被控制使用各種UIViewController機制。

通過做你在做什麼,你違反UIViewController合同。如果你想在UIView/CALayer水平,而不是UIViewController級別使用斑馬線,看在ZXing objc directory類的小工具是沒有得到的東西像viewWillAppearviewDidAppear

2

所以答案是非常簡單的。

我正在使用iOS 5.0和ARC。 ZXing ViewController在方法內本地實例化。由於ViewController本身沒有被查看,ARC在該方法的末尾設置了一個發佈版,並且ViewController被釋放。自ViewController發佈以來,ViewController保留的視圖也將被釋放。由於主ViewController不再存在,因此調用Cancelled,並在nil指針上調用某個方法會導致BAD_ACCESS。

此處的解決方案是將ZXingViewController設置爲全局強屬性。這使得該對象不會在該方法結束時被釋放,因此,只要ViewController處於活動狀態,作爲子視圖添加到另一個ViewControllers視圖的視圖就會保留在內存中。

0

試試這個...也在.h文件中使這個ZXingWidgetController * widController; 並且還對viewScanner將clipToBounds設置爲true。

-(void)viewDidAppear:(BOOL)animated 
    { 
     [super viewDidAppear:animated]; 

     [self performSelector:@selector(openScanner) withObject:nil afterDelay:0.5]; 
    } 

    -(void)openScanner 
    { 
     self.widController = [[ZXingWidgetController alloc] initMiniWithDelegate:self showCancel:NO OneDMode:YES]; 

     NSMutableSet *readers = [[NSMutableSet alloc ] init]; 

     MultiFormatReader* reader = [[MultiFormatReader alloc] init]; 
     [readers addObject:reader]; 

     self.widController.readers = readers; 

     [viewScanner addSubview:self.widController.view]; 
    } 
相關問題