【问题标题】:Removing views with tag from other ViewController从其他 ViewController 中删除带有标签的视图
【发布时间】:2014-04-28 02:16:27
【问题描述】:

我一直试图从其他 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];
        }
    }
}

谢谢 圣地亚哥

【问题讨论】:

  • "view.subviews" 是要循环的视图数组。但请不要这样做。
  • 你的视图是自定义视图??并且您已将其添加到 MainViewController?现在您想在单击按钮时将其从 MainViewController 中删除(此按钮属于您的自定义视图)。对吗?

标签: ios objective-c uiview selector subview


【解决方案1】:

在最后的代码块中,您用作循环迭代器并调用subviewUIView 实例实际上并不代表view 的子视图。以下是你应该如何改变它。

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

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

【讨论】:

  • 他将Target:self 添加到cerarmenu。所以视图=自我。 -> 他无法通过这种方式从超级视图中删除所有视图。唯一的方法是为此 customView 使用委托
  • @VũTuấnAnh 我该怎么做?
【解决方案2】:
+(void)cerrarmenu:(UIView *)view {

 [[view viewWithTag:899] removeFromSuperview];

}

【讨论】:

  • 最好在您的答案中添加一些关于代码的注释,以描述这是如何解决问题的。
【解决方案3】:

我会将您的 viewController 称为“PopUpViewController”。 PopViewController.h:

 @class PopUpViewController;
 @protocol PopUpViewControllerDelegate

     //It is delegate -> notify MainViewController to close PopUpViewController
    -(void)closeWasCalled: (PopUpViewController*)sender; 

 @end

 @interface PopUpViewController: UIViewController{
    //Some variables
 }
 //Your some properties
 //define PopUpViewControlleras delegate
 @property (nonatomic, weak) id <MyClassDelegate> delegate; 

 @end

PopViewController.m:

 -(void)btnClose{ //your close button in PopViewController
      [self.delegate closeWasCalled:self]; //MainViewController will catch that
 }

返回主视图控制器 MainViewController.h:

//Add delegate
@interface PopUpViewController: UIViewController<PopViewControllerDelegate>{
    //Some variables
}
@property (strong,nonatomic) PopUpViewController* pv;

@end

MainViewController.m

//show PopUpViewController where ever you want
//example viewDidLoad

-(void)viewDidLoad{
    self.pv = [[PopViewController alloc]init];
    //set position...
    [self.view addSubView:pv];
    //dont forget set delegate
    pv.delegate = self; -> it very important
}

//using delegate
-(void)closeWasCalled: (PopUpViewController*)pvc {
    [self.pvc removeFromSuperView];
}

我手写这段代码而不是使用 SDK,因为我使用的是 windows。 但这是你可以遵循的方式 如果为此而陷入困境。我会回答更多

此链接是创建委托的教程:How do I create delegates in Objective-C?

【讨论】:

    【解决方案4】:

    如果您在 Menu 类而不是视图控制器中实现方法“cerrarmenu”,那么您需要执行类似的代码

    +(void)cerrarmenu:(UIView *)view {
        UIButton * btn= (UIButton *)sender;
        [[btn superview] removeFromSuperview];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 2016-10-08
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多