0

我已经添加了一个 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的???

4

1 回答 1

0

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

我的解决方案是在调用 release 之前创建一个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];
}
于 2009-08-29T14:01:17.583 回答