我在主窗口上添加了一个自定义视图,然后在视图上创建了一个按钮,并且按钮正在显示,但是在点击的按钮上未执行该操作。
在按钮上点击我要打开一个照相馆。
然后循环创建按钮。

我正在使用的代码在那里...

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;
//  iv.tag=@"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,

         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;

                }
                }

10-06 02:56