简体   繁体   中英

How do I release a parent UIView of/with a UIButton with parameters?

I'm wondering if there is a way to release a UIView, which doesn't exist outside of a specific function via a subViews UIButton. Since I'm not able to give the parameter of the pointer to the UIView trough the button to the IBAction, I'm stuck.

- (IBAction)statsTemp1:(id)sender{


CGRect newSize = CGRectMake(0,13,322,434);
 UIView *overl = [[UIView alloc] initWithFrame:newSize];
 [self.view addSubview:overl];
 [overl setBackgroundColor:[UIColor grayColor]];
 [overl setAlpha:0.85];
 [self doTheGraphIn:overl];
 CGRect btnFrame = CGRectMake(150, 400, 30, 30);
 UIButton *closeStatButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 closeStatButton.frame = btnFrame;
 [overl addSubview:closeStatButton];
 [closeStatButton addTarget:self action:@selector(closeStat:) forControlEvents:UIControlEventTouchUpInside];}-(IBAction)closeStat:(id)sender{
 [self release];
    }
    -(IBAction)closeStat:(id)sender{
 [overl release];
    }

just to claryfy: I want to release the (UIView*)overl with the button and can not create it beforehand.

Thanks for your help, I'd apreciate it :)

It sounds like you want to remove over1 from self.view. (Which implies releasing)

Try this...(from memory, so please check)

- (IBAction)statsTemp1:(id)sender{

// Create UIView
     CGRect newSize = CGRectMake(0,13,322,434);
     UIView *overl = [[UIView alloc] initWithFrame:newSize];
     [overl setBackgroundColor:[UIColor grayColor]];
     [overl setAlpha:0.85];
     [over1 setTag:99];

// Add UIButton to overl
       CGRect btnFrame = CGRectMake(150, 400, 30, 30);
       UIButton *closeStatButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       closeStatButton.frame = btnFrame;
       [overl addSubview:closeStatButton];
       [closeStatButton addTarget:self action:@selector(closeStat:)   forControlEvents:UIControlEventTouchUpInside];

// Add overl to view
     [self.view addSubview:overl];

// over1 can now be released because self.view has a pointer to it
       [overl release];

// Do God knows what
     [self doTheGraphIn:overl];

 }

// Remove overl from self.view
-(IBAction)closeStat:(id)sender {
     UIView *overl = [self.view viewForTag:99];
     [over1 removeFromSuperView];
  }

// The UIButton is autorelease, so you should be good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM