Question

I am using zBar SDK once a qr code detected a delegate will run, inside this delegate I defined a myView and used it as a overlay on the camera with information about that qr code. inside the myView there is a button which calls a method (testAction) but inside this method I cannot acces any of objects which I'd defined in the delegate, how can I access to to myView and objects inside that inside testAction?

- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{

    UIView *myView;
    CGRect frame= CGRectMake(0, 0, 320, 428);
    myView=[[UIView alloc] initWithFrame:frame];
    myView.backgroundColor=[UIColor greenColor];
    myView.alpha=0.7;


    CGRect labelFrame = CGRectMake( 0, 0, 500, 30 );
    UILabel* myLabel = [[UILabel alloc] initWithFrame: labelFrame];

    [myLabel setTextColor: [UIColor orangeColor]];

     NSString *combined = [NSString stringWithFormat:@"%@%@", @"Title: ", symbol.data];
    myLabel.text=combined;


    UIButton * myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(10, 50, 100, 50);
    myButton.tag = 121;
    [myButton setTitle:@"Do Something" forState:UIControlStateNormal];
    [myButton setBackgroundImage:nil forState:UIControlStateNormal];
    [myButton addTarget:self action:@selector(testAction:) forControlEvents:UIControlEventTouchUpInside];

    //[button setBackgroundColor:red];



    [myView addSubview: myLabel];
    [myView addSubview: myButton];

    reader.cameraOverlayView=myView;


}

- (void)testAction: (id)sender{
    //my issue is here
}
Was it helpful?

Solution

Make them properties.

In your interface have:

@property (strong, nonatomic) UIView *myView;
@property (strong, nonatomic) UILabel *myLabel;
@property (strong, nonatomic) UIButton *myButton;

In your delegate method:

CGRect frame= CGRectMake(0, 0, 320, 428);
self.myView=[[UIView alloc] initWithFrame:frame];
self.myView.backgroundColor=[UIColor greenColor];
self.myView.alpha=0.7;

CGRect labelFrame = CGRectMake( 0, 0, 500, 30 );
self.myLabel = [[UILabel alloc] initWithFrame: labelFrame];
self.myLabel.textColor = [UIColor orangeColor];
self.myLabel.text = [NSString stringWithFormat:@"%@%@", @"Title: ", symbol.data];

self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.myButton.frame = CGRectMake(10, 50, 100, 50);
self.myButton.tag = 121;
[self.myButton setTitle:@"Do Something" forState:UIControlStateNormal];
[self.myButton setBackgroundImage:nil forState:UIControlStateNormal];
[self.myButton addTarget:self action:@selector(testAction:) forControlEvents:UIControlEventTouchUpInside];

[self.myView addSubview:self.myLabel];
[self.myView addSubview:self.myButton];

reader.cameraOverlayView = self.myView;

Finally in your action:

- (void)testAction: (id)sender
{
    // self.myView, self.myLabel and self.myButton are all available.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top