简体   繁体   English

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

[英]Which button is pressed in grid view

Evening All 晚上都

I have the following code to fill in a grid of buttons, but how do I detect which button has been selected and pass the button image on another controller 我有以下代码来填写按钮网格,但是如何检测已选择了哪个按钮并将按钮图像传递到另一个控制器上

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

Thanks in advance 提前致谢

You will obtain pressed button as an argument to the callback method (buttonPressed:). 您将获得按下按钮作为回调方法的参数(buttonPressed :)。 Just implement it in such manner: 只需以这种方式实现:

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

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

You could give each button a tag, and then use the -(void)buttonPressed: posted earlier. 您可以给每个按钮一个标签,然后使用-(void)buttonPressed:之前发布。 With that function, you could do something like: 使用该功能,您可以执行以下操作:

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

And so on, and so forth. 等等等等。

I hope that helps! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM