Question

I have one main view as shown in picture. I add 2 subviews into that and each has their own view controller.

In ipadMainViewController,

self.dTVC= [[dialoguesTableViewController alloc] initWithNibName:@"dialoguesTableViewController" bundle:nil];
[self.dTVC.view setFrame:rectFordTVC];
[self.view addSubview:self.dTVC.view];

After that, I want to remove the view of dialoguesTableViewController if I press a button in CategoriesViewController. But, I can't remove it. In CategoriesViewController, I write like this but dialoguesTableViewController can't be removed from ipadMainViewController. How shall I do this?

In CategoriesViewController, I write code like this but it is not working.

self.dTVC= [[dialoguesTableViewController alloc] initWithNibName:@"dialoguesTableViewController" bundle:nil];
[self.dTVC.view removeFromSuperview];

enter image description here

Was it helpful?

Solution

So there are few ways how to do it:

First way:

Add observer to ipadMainViewController initialization method or viewDidLoad method it depends on your needs.

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(buttonPressed)
                                             name:@"kNotificationDidPressedButon"
                                           object:nil];

Add -buttonPressed method to ipadMainViewController controller for removing your view or other your purposes.

- (void)buttonPressed
{
   // remove view here
}

in the CategoriesViewController in the method where you tap on the appropriate button add this code:

[NSNotificationCenter defaultCenter] postNotificationName:@"kNotificationDidPressedButon"
                                                   object:self];

Second way:

Add delegate property to CategoriesViewController. you can find info how to make delegate for example here: link

Third way:

Use objective-c blocks

Initial advice as for beginner:

I suggest you to start from first way, because it is most simplest for understanding. Also you have to remove observer in ipadMainViewControllerr in -dealloc or -viewWillDisapper method, it depends of where you have add observer e.g. in -init method or in -viewDidLoad or -viewWillAppear callback;

[[NSNotificationCenter defaultCenter] removeObserver:self];

OTHER TIPS

try this....

add below code where you can remove view

-(void)viewDidLoad{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeFromSuperview) name:@"REMOVE" object:nil];
}
-(void)removeFromSuperviev{

     [view removeFromSuperview];
}

add below code form you need to remove

[[NSNotificationCenter defaultCenter] postNotificationName:@"REMOVE" object:nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top