2011-03-12 69 views
0

我創建的按鈕的排列,但我沒有看到,按鍵處理觸摸事件(buttonEvent:不調用)UIButton的處理在陣列

這是我的代碼 - 它是不正確的?

- (void)loadView{ 
CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; 
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:screenRect]; 
[backgroundImageView setImage:[UIImage imageNamed:@"background.png"]]; 
backgroundImageView.opaque = YES; 
self.view = backgroundImageView; 
[backgroundImageView release]; 

CGRect brandRect = CGRectMake(90, 25, 140, 70); 
UIImageView *brandImageView = [[UIImageView alloc] initWithFrame:brandRect]; 
[brandImageView setImage:[UIImage imageNamed:@"brand.png"]]; 
[self.view addSubview:brandImageView]; 
buttons = [NSMutableArray array]; 

int y = 100; 
for (int i = 0 ; i < 5; i++){ 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setFrame:CGRectMake(20, y, 280, 50)]; 
    if (i == 0){ 
     [button setBackgroundImage:[UIImage imageNamed:@"select_active.png"] forState:UIControlStateNormal]; 
    } else { 
     [button setBackgroundImage:[UIImage imageNamed:@"select_passive.png"] forState:UIControlStateNormal]; 
    } 
    [button setTitle:[NSString stringWithFormat:@"Object%d",i] forState:UIControlStateNormal]; 
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventAllEvents]; 
    button.tag = 1000 + i ; 
    [self.view addSubview:button]; 
    y += 60; 
} 

-(void)buttonEvent:(id)sender { 
NSLog(@"new button clicked!!!"); 
} 
+0

我找到了解決辦法 - backgroundImageView.userInteractionEnabled = YES解決了我的問題 – dmtrlbdv 2011-03-12 20:34:51

回答

1

您正在爲禁用交互的uiimageview添加按鈕。

另外,您沒有按鈕數組,因爲按鈕是自動釋放的,並且您從不將按鈕添加到按鈕數組中。

+0

非常感謝! – dmtrlbdv 2011-03-12 20:55:09