Objective-C:显示另一个类的值

时间:2015-12-20 13:41:20

标签: ios objective-c

我正在开发iOS应用程序。 我试图从AVFoundation显示检测到的字符串,“BarcodeNum”。但它没有正确显示。以下是我的代码

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

1 个答案:

答案 0 :(得分:0)

SecondView.viewDidLoad创建一个FirstViewController实例并尝试获取其barcodeNum但该怎么办?您刚刚创建了一个新实例 - firstViewController还没有机会设置其barcodeNum属性。

到目前为止,还没有关于SecondViewFirstViewController如何相关或相关的信息。但需要在这些之间建立某种联系。

例如,在SecondView中创建一个包含FirstViewController实例的属性。然后,您展示相同 FirstViewController,然后告诉SecondView请刷新其标签文字。

或者让您的FirstViewController实例进行计算,然后在SecondView手中显示您的prepareForSegue或完整的实例。