2009-08-29 74 views
0

我添加了一个UIViewController的视图和一个按钮,滚动型的子视图...现在我该怎样释放UIViewController的所有实例...这里是在视图中附加的图像alt text http://i25.tinypic.com/2m4yefa.png如何将uiview作为子视图添加到scrollview中?

每一个的UIView有一个按钮,在它进一步导航....

现在我想释放的UIView的,并从滚动视图删除按钮...这里是我的方法来添加子视图 - (空)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]; 
} 

使用此代码subviews scrollview的计数为96。

我已经使用这个方法的dealloc删除子视图,但它没有公布UIvicontroller的

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

我如何释放UIViewController的???

回答

0

当你创建你的UIViewController时,你永远不会释放你自己的所有权,所以直到你自己释放它,它永远不会消失。问题是没有其他东西拥有UIViewController,所以如果你使用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]; 
} 
+0

非常感谢它的工作...请你以更详细的方式 – 2009-08-29 14:34:56

+0

解释你错过了这个[MultiChoiceThumbViewControllerobj版本] 行后面的这行[subViewControllers addObjet:MultiChoiceThumbViewControllerobj]; – 2009-08-29 14:35:45

相关问题