2013-06-12 75 views
-1

我使用zBar SDK一旦qr代碼檢測到一個委託將運行,在這個委託內部,我定義了一個myView並將它用作攝像頭上的覆蓋圖,並提供關於該qr代碼的信息。 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 
} 

回答

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. 
} 
+0

OMG有時大腦只是被鎖定! – user2211254