2011-08-12 37 views
0

我在主窗口上添加一個自定義視圖,然後在視圖上我創建一個按鈕並且按鈕正在顯示,但該操作未在點擊按鈕上執行。 在按鈕上點擊我想打開一個照相館。 並在一個循環中創建按鈕。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]; 
+1

爲什麼要將按鈕添加到圖像視圖? –

+0

因爲我必須在按鈕上顯示圖像而不是標題。 – shivangi

+1

@shivangi:那麼你應該添加圖像作爲按鈕的背景圖像。 – Maulik

回答

0

你好Shivangi,

  I think you want to set image on button so try this code its will help you 



      //function to create button dynamically on scrollview 
      -(void)createButton 
      { 
      int Y = 20; 
      indexRow = 0; 
      for (int i = 0; i < [prow.noOfPhotos count]; i++) 
      { 
       thumbNailButton = [UIButton buttonWithType:UIButtonTypeCustom];  
       int X = 4 + (78 * (i % 4)); 
       NSLog(@"X = %d",X); 

       NSLog(@"Start Y = %d",Y); 
       if (i % 4 == 0) 
       { 
      NSLog(@"%d",i); 
      Y = (70 * verticalImageRowCount); 
      NSLog(@"Y = %d",Y); 
      verticalImageRowCount++; 
      NSLog(@"VerticalImageRowCount= %d"); 
       } 

       CGRect rect = myScrollView.frame; 
       rect.size.width = 100; 
       rect.size.height = Y + 77; 
       myScrollView.contentSize = CGSizeMake(rect.size.width,rect.size.height); 
       thumbNailButton.frame = CGRectMake(X,Y, 62,65); 

       view = [[UIImageView alloc] init]; 

       prow.photo = [prow.noOfPhotos objectAtIndex:indexRow]; 

       imagePath = prow.photo; 
       asyncImageView = [[[AsyncImageView alloc] initWithFrame:CGRectMake(4, 0, 62, 65)] autorelease]; 
       asyncImageView.backgroundColor = [UIColor clearColor]; 
       [view addSubview:asyncImageView]; 

       NSURL *url = [NSURL URLWithString:imagePath]; 
       [asyncImageView loadImageFromURL:url];   
       [view setImage:[UIImage imageNamed:[prow.noOfPhotos objectAtIndex:i]]]; 

       thumbNailButton.tag = i; 
       thumbNailButton.backgroundColor=[UIColor clearColor]; 
        [thumbNailButton addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside]; 
       [thumbNailButton addSubview:view]; 
       [myScrollView addSubview:thumbNailButton]; 
       indexRow+= 1; 

       } 
       } 
+0

後,我的問題仍然是一樣的。 – shivangi

+0

感謝回覆 – shivangi

+0

我想你想實現照片庫功能 –

1

你的按鈕的框架不應該有你的imageview相同的原點x和y。如果你想要它在圖像視圖內,它應該有原點x:0 y:0,並且與你的圖像視圖具有相同的寬度和高度。

原點x,y是相對於它的父項。在你的情況下,imageview。

+0

我同意Nikunj,爲什麼沒有一個圖像作爲背景的按鈕,而不是? – sanna

+0

是的,我做到了,但問題仍然相同。 – shivangi

+0

你buttonPressed方法是什麼樣子? – sanna

0

嘗試在您的buttonPressed中放入一個NSLog:並查看該方法是否被調用。也許該方法正在調用,但照片庫應用程序不會因爲其他原因啓動。

0

如果將按鈕的alpha設置爲0,則用戶無法單擊該按鈕。這意味着該事件不會被觸發。

[button setAlpha:0]; 

這與設置button.hidden = YES的效果相同。

如果你想要一個使用按鈕的不可見的clickzone,將按鈕類型設置爲UIButtonTypeCustom,它應該做的伎倆。