2015-12-20 54 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

我想读取另一个类的变量的值。您的解决方案将如何工作。我有点困惑。 –

相关问题