从其他ViewController中删除带有标记的视图

时间:2014-04-28 02:16:28

标签: ios objective-c uiview selector subview

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

由于 圣地亚哥

4 个答案:

答案 0 :(得分:4)

在最后一段代码中,您用作循环迭代器并调用UIView的{​​{1}}实例实际上并不代表subview的子视图。这是你应该如何改变它。

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

答案 1 :(得分:3)

+(void)cerrarmenu:(UIView *)view {

 [[view viewWithTag:899] removeFromSuperview];

}

答案 2 :(得分:1)

我将把你的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 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?

答案 3 :(得分:0)

如果您正在实施方法&#34; cerrarmenu&#34;在Menu类而不是视图控制器中,您需要执行类似

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