如何发布uiview在scrollview中添加为子视图?

时间:2009-08-29 13:43:17

标签: objective-c iphone xcode uiscrollview subview

我添加了一个UIViewController的视图和一个按钮以scrollview作为子视图...现在我如何发布UIViewController的所有实例...这里是一个附加图像alt text

在视图中,每个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];
 }

使用此代码子视图计数scrollview = 96。

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

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

我如何发布uiviewcontroller的???

1 个答案:

答案 0 :(得分:0)

当你创建UIViewController时,你永远不会释放自己的所有权,所以在你自己释放它之前,它永远不会消失。问题是没有其他任何东西拥有UIViewController s,所以如果您使用autorelease,它们将立即被解除分配。

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