Question

I am not able remove view. I am adding view using addsubview method but when I use RemoveFromSuperView.

// Here I am adding view from login button

medicalViewObject=[[MedicalInfoViewController alloc] initWithNibName:@"MedicalInfoViewController" bundle:nil];
    [self.view addSubview:medicalViewObject.view];

//here I am writing RemoveFromSuperView in another ViewController

-(void)BackToMainView
{
    [self.view removeFromSuperview];
}
Was it helpful?

Solution

It is not working because you are sending removeFromSuperview to the wrong view object. This is what you need:

[medicalViewObject.view removeFromSuperview];

EDIT:

Your question suggests that you have a view (because you did not include the name of this view in your question, I will simply call it MainView) and then you add a subview called medicalViewObject to your MainView. You also have a method call "BackToMainView" which you want to perform the removeFromSuperview function. Your question suggests that some user action in your medicalViewObject (such as a button press) is supposed to call the "BackToMainView" method.

If this is all correct, then my answer above is correct. But based upon your comment, it sounds like you will also need to implement a delegate protocol in your medicalViewObject, and then have your "MainView" adopt the protocol.

In your declaration of the delegate in your medicalViewObject, you need to have a method call like this:

-(void)backButtonWasPressed;

and in the implementation of your MainView it should look something like this:

-(void)backButtonWasPressed
{
    [medicalViewObject.view removeFromSuperview];
}

So now, whatever user action you are using in your medicalViewObject to go back to the main view (whether a button or some other object) it needs to call the following:

[delegate backButtonWasPressed];

Depending on your situation it may look a little different, but this a fairly common way to accomplish what you are trying to do. I hope this helps.

OTHER TIPS

when you call [self.view removeFromSuperview]; you are telling whatever view controller you are in to remove its own view. You should be calling that line from within that exact same MedicalInfoViewController or telling medicalViewObject from the outside to remove its view like [medicalViewObject.view removeFromSuperview];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top