0

UIViewController のビューとスクロールビューのボタンをサブビューとして追加しました...UIViewController のすべてのインスタンスを解放するにはどうすればよいですか...ここに添付画像があります代替テキスト

ビューでは、すべての uiview にさらにナビゲーションするためのボタンがあります....

ここで、uiview のボタンを解放してスクロールビューからボタンを削除したいと考えています。サブビューを追加する方法は次のとおりです。

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、自分の所有権を解放したことはありません。UIViewController問題は、 s を所有しているものは他にないため、 を使用autoreleaseするとすぐに割り当てが解除されることです。

私の解決策は、NSMutableArrayrelease を呼び出す前に、それらを作成して配列に追加することです。次に、すべての を解放する場合は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];
}
于 2009-08-29T14:01:17.583 に答える