从屏幕iOS中删除视图



我有这样的架构:

----- main view    
       |
        ---right bar view
             |
              ----- Navigation Controller view
                     |
                     ------tableView Controller

当我在tableViewController上时,如何从窗口中删除正确的栏视图?

尝试以下:

 [self.view.superview.superview removeFromSuperview];

另一种更清洁的方法将使用通知:您可以向控制主视图的对象发送通知,以便将其删除正确的栏子视图。它会像:

在您的主视图控制器中:

[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(removeRightBar:) 
    name:@"RemoveRightBarNotification"
    object:nil];

- (void) receiveTestNotification:(NSNotification *) notification
{
     <REMOVE SUBVIEW FROM mainView>
}

在您的表观视图控制器中:

[[NSNotificationCenter defaultCenter] 
    postNotificationName:@"RemoveRightBarNotification" 
    object:self];

在这种情况下,通知将在主视图和表观视图控制器之间提供非常松散的连接。

使用标签对要删除的视图,并使用该标签的引用删除视图。
喜欢:
在将正确的栏视图添加到主视图(self.view)添加标签

之前
rightBarView.tag   =   1;

且用于删除pirticular视图write

for(UIView *temp in [self.view subviews]) {
    if (temp.tag == 1) {
        [temp removeFromSuperview];
    }
}

希望这会有所帮助。

最新更新