从其他ViewController

从其他ViewController

我一直在尝试从其他ViewController调用的动作中删除视图,但我不知道该怎么做
这是我的代码:

 + (Menu *)Mostrar:(UIView *)view{
     CGRect IMGFrame = CGRectMake( 5, 20, 70, 70 );
     UIButton *boton=[[UIButton alloc] initWithFrame:IMGFrame];
     [boton setBackgroundImage:[UIImage imageNamed:@"Logo_SuperiorBTN.png"] forState:UIControlStateNormal];
     [boton setBackgroundImage:[UIImage imageNamed:@"Logo_SuperiorBTN.png"] forState:UIControlStateSelected];
     [boton addTarget: self action: @selector(cerrarmenu:) forControlEvents: UIControlEventTouchUpInside];
     [boton setTag:899];
     [view addSubview: boton];
}

这样从我的MainViewController中调用该部分
-(IBAction)menu:(id)sender{
    Menu *hudView = [Menu Mostrar:self.view];
}

然后显示视图,当我尝试使用按钮关闭视图时会崩溃

关闭菜单的代码是
+(void)cerrarmenu:(UIView *)view{
    for (UIView *subView in view) {
        if (subView.tag == 899) {
            [subView removeFromSuperview];
        }
    }
}

谢谢
圣地亚哥

最佳答案

在最后的代码块中,您用作循环迭代器并调用UIViewsubview实例实际上并不表示view的子视图。这是您应该如何更改的方法。

+(void)cerrarmenu:(UIView *)view {
    for (UIView *subView in view.subviews) {    // UIView.subviews
        if (subView.tag == 899) {
            [subView removeFromSuperview];
        }
    }
}

这利用了@property(nonatomic, readonly, copy) NSArray *subviews提供的UIView

关于ios - 从其他ViewController移除带有标签的 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23331498/

10-13 04:00