2009-08-29 84 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

相關問題