PDA

View Full Version : How to NOT have duplicate views




mistergreen2011
Apr 9, 2011, 05:29 PM
Hi,

So I have a button action that instantiate a new view when pressed.

- (void)slideButton:(id)sender {

CGRect sliderFrame = CGRectMake(0.0f, 0.0f, 320.0f, 416.0f);
AnimationView *pickerview = [[[AnimationView alloc] initWithFrame:sliderFrame] autorelease];
[self.view addSubview:pickerview];
[pickerview hideSlide];
[pickerview slideUp];


}


But the problem is if the user press the button multiple times, duplicate views would pop up.
How do I detect if the object view already exists?
I was thinking below but obviously it won't work.


- (void)slideButton:(id)sender {

if(!pickerview) {
CGRect sliderFrame = CGRectMake(0.0f, 0.0f, 320.0f, 416.0f);
AnimationView *pickerview = [[[AnimationView alloc] initWithFrame:sliderFrame] autorelease];
[self.view addSubview:pickerview];
[pickerview hideSlide];
[pickerview slideUp];

}


}



chown33
Apr 9, 2011, 05:43 PM
You could define pickerView as an instance variable of the class where slideButton: is defined.

Or you could look around in the documentation for UIView for some way to access all its subviews, then figure out some way to tag the pickerView with a uniquely identifiable tag (hint: search the UIView docs for a way to tag views). That way, you can look in the view itself to see if the pickerView was added or not.

mistergreen2011
Apr 9, 2011, 07:36 PM
Thanks. Making the 'pickerview' variable not local to the function worked.
I'm now curious about the 'tag' instance property. How is it used?

I tried googling and read it in the document. I haven't seen an application of it.