Spriggsy
Spriggsy

Reputation: 306

Xcode addSubview with unique name

I have a button that creates a UIImageView and allows the user to select a image from the library.

It also increments as var called "amount" by one

If he presses the button again, it does the same. for as many times as he presses it.

    -(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);
}

I need to give each subview a unique name for example subView1,subView2,subView3....... so that I can reference to them later on.

How can I uniquely identify them?

Upvotes: 1

Views: 2063

Answers (5)

Kamen Dobrev
Kamen Dobrev

Reputation: 1371

You could use also custom class if you have only 1 instance of it

private class MyView: UILabel {}

firstSubviewOf(type: MyView.self)

Upvotes: 0

Matthew Frederick
Matthew Frederick

Reputation: 22305

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).

Upvotes: 2

Carl Veazey
Carl Veazey

Reputation: 18363

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.

Upvotes: 1

Husker Jeff
Husker Jeff

Reputation: 857

Every view has an NSInteger property called tag.

subview.tag = amount;

Later on, when you need to reference a particular subview:

referencedSubview = [self.view viewWithTag:x];
if (referencedSubview != nil) {
 //do whatever
} else {
 //No subview with this tag
}

Upvotes: 1

Max B.
Max B.

Reputation: 871

You may use the tag property

[subview setTag:999];

...access the subview by using viewWithTag

[self.view bringSubviewToFront:[subview viewWithTag:999]];

Upvotes: 1

Related Questions