-1

一旦二维码检测到委托将运行,我将使用 zBar SDK,在此委托中我定义了一个 myView 并将其用作相机上的覆盖层,其中包含有关该二维码的信息。在 myView 中有一个按钮,它调用一个方法(testAction),但在这个方法中我无法访问我在委托中定义的任何对象,我如何访问 myView 和 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
}
4

1 回答 1

0

使它们成为属性。

在您的界面中有:

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

在您的委托方法中:

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;

最后在你的行动中:

- (void)testAction: (id)sender
{
    // self.myView, self.myLabel and self.myButton are all available.
}
于 2013-06-12T23:03:00.417 回答