我在主窗口上添加一個自定義視圖,然後在視圖上我創建一個按鈕並且按鈕正在顯示,但該操作未在點擊按鈕上執行。 在按鈕上點擊我想打開一個照相館。 並在一個循環中創建按鈕。UIButton選擇器不能在mainwindow視圖上工作
代碼,我現在用的就是那裏......
UIImage *image=[[UIImage alloc] initWithData:data cache:NO];
UIImageView *iv=[[UIImageView alloc] initWithFrame:CGRectMake(200, y, 75, 75)];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(200, y, 75, 75)];
[button setAlpha:0];
[button setTag:i];//change this to your loop counter
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
iv.image=image;
// [email protected]"bigimage.jpg";
iv.userInteractionEnabled=YES;
button.userInteractionEnabled = YES;
[iv addSubview:button];
[button release];
y=y+145;
[view1 addSubview:iv];
[iv release];
而且也試試這個代碼,但沒有改善是有.....
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(200, y, 75, 75);
UIImage *img = [[UIImage alloc] initWithData:data];
[button setBackgroundImage:img forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
y = y+145;
[view1 addSubview:button];
爲什麼要將按鈕添加到圖像視圖? –
因爲我必須在按鈕上顯示圖像而不是標題。 – shivangi
@shivangi:那麼你應該添加圖像作爲按鈕的背景圖像。 – Maulik