2012-03-30 97 views
1

我使用两个块动态地在子视图上绘制一些按钮。一个计算纵向模式的帧,另一个计算横向。它运作良好,但是当我旋转时,它会覆盖旧的。因此,我的一些按钮来了两次。这是我的代码检测oriantation:如何在绘制前清除子视图的内容

//i have defined blocks in viewDidLoad: and everything is ok till here 
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
     if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
     { 
      dispatch_async(dispatch_get_main_queue(), PortraitBlock);   
     }  
     else 
     { 
      dispatch_async(dispatch_get_main_queue(), LandscapeBlock); 
       } 
     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || 
       interfaceOrientation == UIInterfaceOrientationPortrait || 
       interfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
       interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown); 
    } 

现在,我该如何清理视图我添加按钮?

注:我补充一个UIView对象上的按钮,而该对象也就是一个UIScrollView对象

+0

@illis如果按钮被添加了两次,然后给按钮添加标签并检查按钮是否存在然后dnt添加它 – Leena 2012-03-30 13:45:45

+0

每次旋转设备时都不需要重新绘制按钮。只要将它们移动到合适的位置并在需要时改变外观。 – SVGreg 2012-03-30 13:55:30

+0

@SVGreg我该怎么做,? – ilhnctn 2012-03-30 14:03:32

回答

5

喜,

试试下面的代码与此你所有你认为 ALLOC将删除按钮。

for(UIButton *view in yourview.subviews) 
    { 
     [view removeFromSuperview]; 
    } 
+0

我确实尝试了这个块,后来在旋转方法。但它会删除所有的滚动视图。出现空白屏幕 – ilhnctn 2012-03-30 13:47:51

+0

这将从您的视图中删除所有子视图。 – SVGreg 2012-03-30 16:58:04

+0

用某个标记值标记按钮的视图以找到它们。 – SVGreg 2012-03-30 16:58:54

1

不要添加新的按钮都上。只要改变旧的框架。

如果你需要添加新的按钮。只删除旧的!?要删除所有子视图,你可以使用:

for(UIView* view in self.view.subviews) 
{ 
    [view removeFromSuperview]; 
} 
+0

我试过了,但第二次旋转后崩溃 – ilhnctn 2012-03-30 13:30:17

+0

任何UI更改都必须在主线程上完成,为什么要异步调用这些块?什么是崩溃!?任何日志? – calimarkus 2012-03-30 13:33:30

+0

我尝试了你的代码在我的块内,但空的屏幕出现。没有崩溃,但没有结果 – ilhnctn 2012-03-30 13:36:22

2

从您的视图中删除的意见,先检查类,然后将其删除之前。之后,您可以根据方向在屏幕上添加按钮。

for(UIView *view in [View to add buttons].subviews) 
{ 
    if ([view isKindOfClass:[UIButton class]]) [view removeFromSuperview]; 
} 

如果你使用任何customButton中提到你customButtonClassName代替UIButton