Objective c 动态写入UIImageView名称

Objective c 动态写入UIImageView名称,objective-c,ios,Objective C,Ios,在下面的if语句中,我如何使piece1的名称是动态的,以便它也检查其他片段,即piece1、piece2、…piece5 我必须让它看起来像[@“piece%d”,我]我只是不确定写这篇文章的正确方式是什么,因为我刚刚开始使用iOS for (int i = 0; i < 6; i++) { if(CGRectContainsPoint([piece1 frame], location)){ piece1.center = location; } }

在下面的
if
语句中,我如何使piece1的名称是动态的,以便它也检查其他片段,即piece1、piece2、…piece5

我必须让它看起来像[@“piece%d”,我]我只是不确定写这篇文章的正确方式是什么,因为我刚刚开始使用iOS

for (int i = 0; i < 6; i++) {

    if(CGRectContainsPoint([piece1 frame], location)){
        piece1.center = location;  
    }
}
for(int i=0;i<6;i++){
if(CGRectContainsPoint([piece1帧],位置)){
1.中心=位置;
}
}
类似这样的内容:

for (int i = 0; i < 6; i++) {
    // Create a selector to get the piece
    NSString *selectorName = [NSString stringWithFormat:@"piece%d", i];
    SEL selector = NSSelectorFromString(selectorName);
    UIView *peice = [self performSelector:selector];

    // Now do your test
    if(CGRectContainsPoint([piece frame], location)){
        piece1.center = location;  
    }
}
for(int i=0;i<6;i++){
//创建选择器以获取工件
NSString*selectorName=[NSString stringWithFormat:@“工件%d”,i];
SEL selector=NSSelectorFromString(selectorName);
UIView*peice=[自执行选择器:选择器];
//现在做你的测试
if(CGRectContainsPoint([工件框架],位置)){
1.中心=位置;
}
}
关键位是
nsselector或fromstring
以将字符串转换为选择器,以及
performSelector:
以获取与之匹配的对象。

类似于以下内容:

for (int i = 0; i < 6; i++) {
    // Create a selector to get the piece
    NSString *selectorName = [NSString stringWithFormat:@"piece%d", i];
    SEL selector = NSSelectorFromString(selectorName);
    UIView *peice = [self performSelector:selector];

    // Now do your test
    if(CGRectContainsPoint([piece frame], location)){
        piece1.center = location;  
    }
}
for(int i=0;i<6;i++){
//创建选择器以获取工件
NSString*selectorName=[NSString stringWithFormat:@“工件%d”,i];
SEL selector=NSSelectorFromString(selectorName);
UIView*peice=[自执行选择器:选择器];
//现在做你的测试
if(CGRectContainsPoint([工件框架],位置)){
1.中心=位置;
}
}

关键位是
nsselector或fromstring
以将字符串转换为选择器,以及
performSelector:
以获取与之匹配的对象。

初始化标记时,首先将标记添加到图像视图中 然后将代码更改为

for (int i = 0; i < 6; i++) {

 UIImageView *piece = (UIImageView *)[self.view viewWithTag:i] ;

    if(CGRectContainsPoint([piece frame], location)){
        piece.center = location;  
    }
}
for(int i=0;i<6;i++){
UIImageView*工件=(UIImageView*)[self.view视图带标记:i];
if(CGRectContainsPoint([工件框架],位置)){
工件中心=位置;
}
}

确保标记与循环i值中的值匹配。

初始化标记时,首先将标记添加到imageView中 然后将代码更改为

for (int i = 0; i < 6; i++) {

 UIImageView *piece = (UIImageView *)[self.view viewWithTag:i] ;

    if(CGRectContainsPoint([piece frame], location)){
        piece.center = location;  
    }
}
for(int i=0;i<6;i++){
UIImageView*工件=(UIImageView*)[self.view视图带标记:i];
if(CGRectContainsPoint([工件框架],位置)){
工件中心=位置;
}
}

确保您的标记与循环i值中的值匹配。

您可能需要稍微调整一下-您的第一个标记将是
0
,这是所有未标记视图的默认标记。尝试使用标记查看:i+100并标记您的作品100、101、102…;)您可能需要对此进行一些调整—您的第一个标记将是
0
,这是所有未标记视图的默认标记。尝试使用标记查看:i+100并标记您的作品100、101、102…;)