在网格视图中按下哪个按钮

时间:2013-01-10 22:27:07

标签: iphone ios gridview uibutton

全晚候

我有以下代码填写按钮网格,但如何检测选择了哪个按钮并在另一个控制器上传递按钮图像

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 ];

提前致谢

2 个答案:

答案 0 :(得分:1)

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

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

    UIImage *image = [senderButton backgroundImageForState:UIControlStateNormal];
    //use image:)
}

答案 1 :(得分:0)

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

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

等等,等等。

我希望有所帮助!