Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在网格视图中按下哪个按钮_Iphone_Ios_Gridview_Uibutton - Fatal编程技术网

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

Iphone 在网格视图中按下哪个按钮,iphone,ios,gridview,uibutton,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] initWith

晚上好

我有下面的代码来填充按钮网格,但是我如何检测哪个按钮被选中并将按钮图像传递到另一个控制器上呢

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 ];
i=0;
int i1=0;

while(i您将获得pressed button作为回调方法的参数(buttonPressed:)。 就这样实施吧:

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

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

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

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


我希望这会有所帮助!

抱歉,请为您的代码添加一条注释。.只在主线程中使用[NSData DATAHETCONTENTSOFURL:]是不好的做法,因为它是同步的。.这种方法可能会导致性能不好。最好使用UIImageView从URL显示图像(它有方法setImageWithURL,异步加载数据)谢谢你的提示Alexander我知道,我有一个按钮网格,每个按钮都是一个与照片应用程序非常相似的数组中的图像,我想知道如何发现按钮被按下,以便我可以将该按钮图像推到另一个viewController