How to NOT have duplicate views

Discussion in 'iPhone/iPad Programming' started by mistergreen2011, Apr 9, 2011.

  1. macrumors member

    Joined:
    Mar 23, 2011
    #1
    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]
      
        
    }
    
    
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    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.
     
  3. thread starter macrumors member

    Joined:
    Mar 23, 2011
    #3
    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.
     

Share This Page