需要一些关于如何构建/使用委托的帮助

时间:2014-06-30 12:35:22

标签: ios delegates

我在代表创建和使用方面苦苦挣扎。有人能帮助我理解我做错了什么吗?根据我读过的所有例子,这是正确的,但我的数据没有被退回。

ViewController(Parent)

·H

    #import <UIKit/UIKit.h>
#import "BarcodeViewController.h"

@interface ViewController: UIViewController <BarcodeViewDelegate> {
    IBOutlet UILabel *bcode;
}
@end

的.m

-(void)setbarcode:(NSString*)barcode
{
    NSLog(@" data %@", barcode);
    bcode.text = barcode;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"scanbarcode"])
    {

        BarcodeViewController *cv = [segue destinationViewController];
        cv.delegate = self;
    }
}

条形码视图控制器(子视图)

·H

@protocol BarcodeViewDelegate <NSObject>

-(void)setbarcode:(NSString*)barcode;

@end

@interface BarcodeViewController : UIViewController
{
    id<BarcodeViewDelegate> delegate;
}

@property(nonatomic,assign)id delegate;

@end

的.m

#import "BarcodeViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface BarcodeViewController () <AVCaptureMetadataOutputObjectsDelegate>
{
    AVCaptureSession *_session;
    AVCaptureDevice *_device;
    AVCaptureDeviceInput *_input;
    AVCaptureMetadataOutput *_output;
    AVCaptureVideoPreviewLayer *_prevLayer;

    UIView *_highlightView;
    UIImageView *_imageOverlay;
}
@end

@implementation BarcodeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];


    /*
     * setup scanner view
     */
    _highlightView = [[UIView alloc] init];
    _highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
    _highlightView.layer.borderColor = [UIColor greenColor].CGColor;
    _highlightView.layer.borderWidth = 3;
    [self.view addSubview:_highlightView];

    /*
     * setup a overlay guide
     */
    _imageOverlay = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"camera_overlay"]];
    [self.view addSubview:_imageOverlay];


    _session = [[AVCaptureSession alloc] init];
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;

    _input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];
    if (_input) {
        [_session addInput:_input];
    } else {
        NSLog(@"Error: %@", error);
    }

    _output = [[AVCaptureMetadataOutput alloc] init];
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [_session addOutput:_output];

    _output.metadataObjectTypes = [_output availableMetadataObjectTypes];

    _prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    _prevLayer.frame = self.view.bounds;
    _prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:_prevLayer];

    [_session startRunning];

    [self.view bringSubviewToFront:_highlightView];
    [self.view bringSubviewToFront:_imageOverlay];
}

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    CGRect highlightViewRect = CGRectZero;
    AVMetadataMachineReadableCodeObject *barCodeObject;
    NSString *detectionString = nil;
    NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
                              AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
                              AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];
    /*
     * keep looking around while we look for the barcode
     */
    for (AVMetadataObject *metadata in metadataObjects) {
        for (NSString *type in barCodeTypes) {
            if ([metadata.type isEqualToString:type])
            {
                barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
                //highlightViewRect = barCodeObject.bounds;
                detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
                break;
            }
        }

        if (detectionString != nil)
        {
            /*
             * Set the detected barcode so we can use it.
             * - perform segway to another view
             */
            //barcode = detectionString;
            //NSLog(@" data %@", detectionString);

            [delegate setbarcode: detectionString];
            [self.navigationController popViewControllerAnimated:YES];

            break;
        }
        //else
        /*
         * Just reset the barcode value for now
         */
            //_barcode = false;
    }

    _highlightView.frame = highlightViewRect;
}

@end

1 个答案:

答案 0 :(得分:0)

你的问题很简单。

您定义一个实例变量一个属性来保存委托:     {         id代表;     }

@property(nonatomic,assign)id delegate;

您可以通过属性设置委托:

cv.delegate = self;

然后通过实例变量访问它:

[delegate setbarcode: detectionString]; 

该属性由另一个实例变量支持,该变量自动定义为_delegate,您不应该访问该变量。您应该始终通过该属性访问self.delegate。当你的代表ivar打电话时,它将是零,所以没有任何东西被送回。

删除不必要的实例变量声明,正确键入属性(id<BarcodeViewDelegate>而不仅仅是id)并始终通过属性访问它,您就可以了。