xcode 如何释放在滚动视图中添加为子视图的 uiview?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1351473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to release uiview added as subview in scrollview?
提问by Rahul Vyas
i have added a UIViewController's view and a button to scrollview as subview...now how do i release all instance of UIViewController's...here is an attached image
我已经添加了一个 UIViewController 的视图和一个按钮来滚动视图作为子视图......现在我如何释放 UIViewController 的所有实例......这是一个附加的图像
in the view every uiview has a button over it for further navigation....
在视图中,每个 uiview 上都有一个按钮,用于进一步导航....
now i want to release uiview's and remove buttons from the scrollview ...here is my method to add subviews -(void)AddOneButton:(NSInteger)myButtonTag { lastButtonNumber = lastButtonNumber + 1;
现在我想释放 uiview 并从滚动视图中删除按钮......这是我添加子视图的方法 -(void)AddOneButton:(NSInteger)myButtonTag { lastButtonNumber = lastButtonNumber + 1;
if ((lastButtonNumber == 1) || ((lastButtonNumber%2) == 1)) {
btnLeft = 8;}
else if ((lastButtonNumber == 2) || ((lastButtonNumber%2) == 0)) {
btnLeft = 162;
}
CGRect frame1 = CGRectMake(btnLeft, btnTop, 150, 150);
CGRect frame2 = CGRectMake(btnLeft, btnTop, 150, 150);
UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
Button.frame = frame1;
Button.tag = myButtonTag;
[Button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[Button setBackgroundColor:[UIColor clearColor]];
[Button setBackgroundImage:[UIImage imageNamed:@"WaitScreen.png"] forState:UIControlStateHighlighted];
[Button setBackgroundImage:[UIImage imageNamed:@"WaitScreen.png"] forState:UIControlStateSelected];
if(categoryType==1)
{
MultiChoiceThumbViewControllerobj = [[MultiChoiceThumbViewController alloc] initWithPageNumber:myButtonTag+1];
MultiChoiceThumbViewControllerobj.view.frame=frame2;
MultiChoiceThumbViewControllerobj.lblCounter.text=[NSString stringWithFormat:@"%d of %d",myButtonTag+1,flashCardsId.count];
MultiChoiceThumbViewControllerobj.lblQuestion.text=[flashCardText objectAtIndex:myButtonTag];
[myScrollView addSubview:MultiChoiceThumbViewControllerobj.view];
}
[myScrollView addSubview:Button];
}
with this code subviews count of scrollview is=96.
使用此代码,滚动视图的子视图计数为 = 96。
i have used this method in dealloc to remove subviews but it is not releasing UIvicontroller's
我在 dealloc 中使用了这个方法来删除子视图,但它没有释放 UIvicontroller 的
for(UIView *subview in [myScrollView subviews])
{
[subview removeFromSuperview];
NSLog(@"Subviews Count=%d",myScrollView.subviews.count);
}
how do i release uiviewcontroller's???
我如何释放 uiviewcontroller 的???
采纳答案by Ed Marty
When you created your UIViewController
s you never released your own ownership of it, so until you release it yourself, it will never go away. The problem is that nothing else owns the UIViewController
s, so if you used autorelease
, they would be deallocated right away.
当你创建你的UIViewController
s 时,你从未释放过你自己的所有权,所以直到你自己释放它,它永远不会消失。问题是没有其他东西拥有UIViewController
s,所以如果你使用了autorelease
,它们会立即被释放。
My solution would be to create an NSMutableArray
and add them to the array before calling release on them. Then, when you want to release all of your UIViewController
s, remove them from the array. NSMutableArray
retains ownership of objects you add to it and releases ownership upon removal or deallocation. Something like this:
我的解决方案是NSMutableArray
在对它们调用 release 之前创建一个并将它们添加到数组中。然后,当您想释放所有UIViewController
s 时,请将它们从数组中删除。 NSMutableArray
保留您添加到其中的对象的所有权,并在删除或解除分配时释放所有权。像这样的东西:
-(id) init {
subViewControllers = [NSMutableArray new];
}
...
-(void)AddOneButton:(NSInteger)myButtonTag {
...
if(categoryType==1) {
MultiChoiceThumbViewControllerobj = [[MultiChoiceThumbViewController alloc] initWithPageNumber:myButtonTag+1];
MultiChoiceThumbViewControllerobj.view.frame=frame2;
MultiChoiceThumbViewControllerobj.lblCounter.text=[NSString stringWithFormat:@"%d of %d",myButtonTag+1,flashCardsId.count];
MultiChoiceThumbViewControllerobj.lblQuestion.text=[flashCardText objectAtIndex:myButtonTag];
[myScrollView addSubview:MultiChoiceThumbViewControllerobj.view];
[subViewControllers addObjet:MultiChoiceThumbViewControllerobj];
[MultiChoiceThumbViewControllerobj release];
}
[myScrollView addSubview:Button];
}
...
- (void) removeSuperviews {
for(UIView *subview in [myScrollView subviews]) {
[subview removeFromSuperview];
NSLog(@"Subviews Count=%d",myScrollView.subviews.count);
}
[subViewControllers removeAllObjects];
}