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.
精彩评论