【问题标题】:how to release uiview added as subview in scrollview?如何释放在滚动视图中添加为子视图的uiview?
【发布时间】:2009-08-29 13:43:17
【问题描述】:

我已经添加了一个 UIViewController 的视图和一个按钮来滚动视图作为子视图...现在我如何释放 UIViewController 的所有实例...这是一个附加的图像

在视图中,每个 uiview 上方都有一个按钮,用于进一步导航......

现在我想释放 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];
 }

使用此代码,滚动视图的子视图计数为=96。

我在 dealloc 中使用了这个方法来删除子视图,但它没有释放 UIvicontroller 的

     for(UIView *subview in [myScrollView subviews])
    {
        [subview removeFromSuperview];
        NSLog(@"Subviews Count=%d",myScrollView.subviews.count);
    }

如何释放uiviewcontroller的???

【问题讨论】:

    标签: objective-c iphone xcode uiscrollview subview


    【解决方案1】:

    当您创建 UIViewControllers 时,您从未发布过您自己的所有权,因此在您自己发布之前,它永远不会消失。问题是没有其他人拥有UIViewControllers,因此如果您使用autorelease,它们将立即被释放。

    我的解决方案是创建一个NSMutableArray 并将它们添加到数组中,然后再调用它们的释放。然后,当您想要释放所有 UIViewControllers 时,将它们从阵列中移除。 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];
    }
    

    【讨论】:

    • 非常感谢它的工作...你能更详细地解释一下吗
    • 你错过了这个 [MultiChoiceThumbViewControllerobj 版本];此行之后的行 [subViewControllers addObjet:MultiChoiceThumbViewControllerobj];
    猜你喜欢
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    相关资源
    最近更新 更多