本文介紹了如何從滾動視圖中刪除子視圖?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何從我的滾動視圖中刪除所有子視圖...
how do i remove all subviews from my scrollview...
我在滾動視圖中有一個 uiview 和它上方的按鈕,如下所示....
i have a uiview and a button above it in the scrollview something like this....
這是我在滾動視圖中添加子視圖的代碼
here is my code to add subview in scroll view
-(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];
GraphThumbViewControllerobj = [[GraphThumbViewController alloc] initWithPageNumber:[[GraphIdArray objectAtIndex:myButtonTag]intValue]];
GraphThumbViewControllerobj.view.frame=frame2;
GraphThumbViewControllerobj.lblCounter.text=[NSString stringWithFormat:@"%d of %d",myButtonTag+1,flashCardsId.count];
GraphThumbViewControllerobj.lblQuestion.text=[flashCardText objectAtIndex:myButtonTag];
[myScrollView addSubview:GraphThumbViewControllerobj.view];
[myScrollView addSubview:Button];
if ((lastButtonNumber == 2) || ((lastButtonNumber%2) == 0)) {
btnTop = btnTop + 162;
}
if (btnTop+150 > myScrollView.frame.size.height) {
myScrollView.contentSize = CGSizeMake((myScrollView.frame.size.width), (btnTop+160));}
}
這是刪除子視圖的代碼
if(myScrollView!=nil)
{
while ([myScrollView.subviews count] > 0) {
//NSLog(@"subviews Count=%d",[[myScrollView subviews]count]);
[[[myScrollView subviews] objectAtIndex:0] removeFromSuperview];
}
推薦答案
要從任何視圖中刪除所有子視圖,您可以遍歷子視圖并為每個子視圖發(fā)送 removeFromSuperview
調用:
To remove all the subviews from any view, you can iterate over the subviews and send each a removeFromSuperview
call:
// With some valid UIView *view:
for(UIView *subview in [view subviews]) {
[subview removeFromSuperview];
}
不過,這完全是無條件的,并且會刪除給定視圖中的所有子視圖.如果你想要更細粒度的東西,你可以采取幾種不同的方法:
This is entirely unconditional, though, and will get rid of all subviews in the given view. If you want something more fine-grained, you could take any of several different approaches:
- 維護您自己的不同類型的視圖數組,以便稍后以相同的方式向它們發(fā)送
removeFromSuperview
消息 - 保留您創(chuàng)建它們的所有視圖并保留指向這些視圖的指針,以便您可以根據需要單獨發(fā)送它們
removeFromSuperview
- 在上述循環(huán)中添加
if
語句,檢查類是否相等.例如,要僅刪除視圖中存在的所有 UIButton(或 UIButton 的自定義子類),您可以使用以下內容:
- Maintain your own arrays of views of different types so you can send them
removeFromSuperview
messages later in the same manner - Retain all your views where you create them and hold on to pointers to those views, so you can send them
removeFromSuperview
individually as necessary - Add an
if
statement to the above loop, checking for class equality. For example, to only remove all the UIButtons (or custom subclasses of UIButton) that exist in a view, you could use something like:
// Again, valid UIView *view:
for(UIView *subview in [view subviews]) {
if([subview isKindOfClass:[UIButton class]]) {
[subview removeFromSuperview];
} else {
// Do nothing - not a UIButton or subclass instance
}
}
這篇關于如何從滾動視圖中刪除子視圖?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!