2015-12-20 24 views
0

我正在使用iOS應用程序。 我試圖顯示從AVFoundation,「BarcodeNum」檢測到的字符串。但它沒有正確顯示。下面是我的代碼Objective-C:顯示另一個類的值

FirstViewController.h

#import <UIKit/UIKit.h> 
@interface FirstViewController : UIViewController 
@property (strong, nonatomic) NSString *detectionString; 
@property (strong, nonatomic) NSString *barcodeNum; 
@end 

FirstViewController.m

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection 
{ 
CGRect highlightViewRect = CGRectZero; 
AVMetadataMachineReadableCodeObject *barCodeObject; 
self.detectionString = nil; 
NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code, 
          AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code, 
          AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode]; 

for (AVMetadataObject *metadata in metadataObjects) { 
    for (NSString *type in barCodeTypes) { 
     if ([metadata.type isEqualToString:type]) 
     { 
      barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata]; 
      highlightViewRect = barCodeObject.bounds; 
      self.detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue]; 

      UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Barcode Detected" message:self.detectionString preferredStyle:UIAlertControllerStyleAlert]; 

      UIAlertAction* yes = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { 
       FirstViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"new"]; 

       [self presentViewController:vc animated:YES completion:nil]; 
      }]; 

      UIAlertAction* no = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; 

      [alert addAction:yes]; 
      [alert addAction:no]; 

      [self presentViewController:alert animated:YES completion:nil]; 


      break; 

     } 

    } 

    if (self.detectionString != nil) 
    { 
     _label.text = self.detectionString; 
     self.barcodeNum = self.detectionString; 
     break; 


    } 
    else 
     _label.text = @"Barcode not found"; 
} 

_highlightView.frame = highlightViewRect; 
} 

SecondView.m

#import "SecondView.h" 
#import "FirstViewController.h" 
#import <Foundation/Foundation.h> 


@interface SecondView() 
@property (strong, nonatomic) IBOutlet UILabel *label; 
@end 

@implementation SecondView 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; 
    _label.text=[NSString stringWithFormat:@"%@",firstViewController.barcodeNum]; 
    //Not displaying the barcode number detected 
} 
@end 
+0

您正在轉向FirstViewController,然後您爲什麼要檢查secondView中的條碼值?你如何轉向第二種觀點? –

+0

如果您想從其他類訪問它們,您需要在.h文件中聲明屬性 –

+0

@ Mr.T我正在通過AlertController按鈕中的模態進行移動。我正在尋找將在FirstView中檢測到的值顯示在SecondView中的標籤中。我在FirstView「h」文件中聲明瞭barcodeNum。我沒有發佈任何代碼 –

回答

0

SecondView.viewDidLoad創建一個FirstViewController實例,並且嘗試得到它的barcodeNum但這應該如何工作?您剛剛創建了一個新實例 - 第一個ViewController尚未有機會設置其屬性barcodeNum

到目前爲止,還沒有關於SecondViewFirstViewController如何相關或連接的信息。但需要建立這些之間的某種連接。

例如,在SecondView中創建一個屬性,其中包含FirstViewController的一個實例。然後您提出相同的FirstViewController然後告訴SecondView請刷新其標籤文本。

或者讓您的FirstViewController實例進行計算,然後出示SecondViewprepareForSegue手或者您的barcodeNum或完整的實例結束。

+0

因此,要回答你的問題,FirstView和SecondView通過模態連接。當相機檢測到條形碼時,它會顯示一個警報控制器,顯示條形碼並詢問用戶是或否。如果是,則顯示SecondView。在SecondView中我有一個UILabel,我想要顯示條形碼。 –

+0

好的,那麼適合最後一段,在'SecondView'和'SecondView'中從該變量中讀取'barcodeNum'或'firstViewController'屬性而不是創建一個新的'FirstViewController' – luk2302

+0

我想讀取另一個類的變量的值。您的解決方案將如何工作。我有點困惑。 –

相關問題