0

晚上所有

我有以下代码来填充按钮网格,但是如何检测已选择哪个按钮并将按钮图像传递到另一个控制器

i =0; 
int i1=0; 
while(i<n){ 
int yy = 4 +i1*79; 
for(int j=0; j<4;j++){ 
if (i>=n) break; 
CGRect rect; 
rect = CGRectMake(4+79*j, yy, 75, 75); 
UIButton *button=[[UIButton alloc] initWithFrame:rect]; 
[button setFrame:rect]; 

id item = [items objectAtIndex:i]; 
NSString *imageLink = [item objectForKey:@"link"]; 

UIImage *buttonImageNormal=[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURLURLWithString: imageLink]]]; 

[button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal]; 
button.tag =i; 
NSLog(@"index: %i", i); 
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside ];

提前致谢

4

2 回答 2

1

您将获得按下的按钮作为回调方法 (buttonPressed:) 的参数。只需以这种方式实现它:

- (void)buttonPressed:(UIButton *)senderButton {

    UIImage *image = [senderButton backgroundImageForState:UIControlStateNormal];
    //use image:)
}
于 2013-01-10T22:33:34.467 回答
0

您可以给每个按钮一个标签,然后使用-(void)buttonPressed:之前发布的。使用该功能,您可以执行以下操作:

- (void)buttonPressed:(UIButton *)senderButton 
{
    if (senderButton.tag == 0) {
         // perform segue
    } else if (senderButton.tag == 1) {
         // perform other segue
    }
}

等等等等。

我希望这会有所帮助!

于 2013-01-11T00:29:20.607 回答