Question

I've been trying to remove my view from an action called from other ViewController but I don't know how to do it Here is my code:

 + (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];
}

That part is called from my MainViewController like this

-(IBAction)menu:(id)sender{
    Menu *hudView = [Menu Mostrar:self.view];
}

Then it shows the view and when I try to close it using the button it crashes

The code to close the menu is

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

Thanks Santiago

Was it helpful?

Solution

In the final block of code, the UIView instance you are using as the loop iterator and calling subview is not actually representative of the subviews of view. Here is how you should change it.

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

This takes advantage of the @property(nonatomic, readonly, copy) NSArray *subviews provided by UIView.

OTHER TIPS

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

 [[view viewWithTag:899] removeFromSuperview];

}

I will call your viewController as "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
 }

return to 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];
}

I hand write this code and not using SDK because im using windows. But this is the way you can follow If get stuck for this. I will answer more

This link is tutorial for create delegate: How do I create delegates in Objective-C?

If you are implementing method "cerrarmenu" in Menu class instead of view controller then you need to do code like

+(void)cerrarmenu:(UIView *)view {
    UIButton * btn= (UIButton *)sender;
    [[btn superview] removeFromSuperview];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top