1

我有一个创建 UIImageView 并允许用户从库中选择图像的按钮。

它还以 var 的形式递增,称为“数量”

如果他再次按下按钮,它会做同样的事情。他按了多少次。

    -(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{



    UIImageView *subView;    
    CGRect frame = CGRectMake(60, 100, 200, 200);
    subView = [[UIImageView alloc] initWithFrame:frame];



    NSString *mediaType = [info
                           objectForKey:UIImagePickerControllerMediaType];
    [self dismissModalViewControllerAnimated:YES];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = [info 
                          objectForKey:UIImagePickerControllerOriginalImage];


        subView.image = image;
        if (newMedia)
            UIImageWriteToSavedPhotosAlbum(image, 
                                           self,
                                           @selector(image:finishedSavingWithError:contextInfo:),
                                           nil);
    }
    else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {
        // Code here to support video if enabled
    }

    [self.view addSubview:subView];  
    amount += 1;

    NSLog(@"%d", amount);
}

我需要给每个子视图一个唯一的名称,例如 subView1,subView2,subView3....... 以便我以后可以引用它们。

我怎样才能唯一地识别它们?

4

4 回答 4

2

You can't give them names. You have a couple of options for easily identifying them later.

The first is to subclass UIVIew and add an NSString property where you store the name. You can then iterate through the parent view's subviews, check to ensure the subview you're looking at it is of your new subclass, and then check it's name. The major advantage of this method is that your subviews can have whatever identifying data you want. A disadvantage is the need to check for class type, etc... slightly clunky, but often better coding.

The other option is to use UIView's tag property, which is an NSInteger. You can give each subview its own tag. Two advantages to this system: it's built into UIView, and you can use the parent view's viewWithTag method to find a particular subview (though that's somewhat risky due to the fact that tags aren't unique by default and nothing prevents you from assigning the same tag to multiple subviews).

于 2012-10-03T19:54:06.203 回答
1

您可以使用标签属性

[subview setTag:999];

...通过使用访问子视图viewWithTag

[self.view bringSubviewToFront:[subview viewWithTag:999]];
于 2012-10-03T19:43:55.303 回答
1

每个视图都有一个名为 的 NSInteger 属性tag

subview.tag = amount;

稍后,当您需要引用特定的子视图时:

referencedSubview = [self.view viewWithTag:x];
if (referencedSubview != nil) {
 //do whatever
} else {
 //No subview with this tag
}
于 2012-10-03T19:45:33.187 回答
1

You could use an NSMutableDictionary and then create an NSString from the amount variable - like [NSString stringWithFormat:@"view%d",amount]. Then use this name to serve as the key for your view. This is guaranteed to not run into any potential conflicts with tag numbering, and will let you keep track of views across view hierarchies as well, if you need to.

于 2012-10-03T19:49:46.543 回答