Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mistergreen2011

macrumors member
Original poster
Mar 23, 2011
36
0
Hi,

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

Code:
- (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.

Code:
- (void)slideButton:(id)sender {
    
[B]if(!pickerview) {[/B]
    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];

[B]}[/B]
  
    
}
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
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

macrumors member
Original poster
Mar 23, 2011
36
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.