简体   繁体   中英

Interact with Parent view controller when selecting a table cell in a container view

I have this problem that I want to hide the container view from the parent VC when I tap a table cell at the last VC on the right.

在此处输入图片说明

I tried hooking up a protocol delegate but it did not work.

Here is my failed attempt:

  1. I added the Protocol on the GOCategoryMenuViewController.

     @protocol GOCategoryMenuViewControllerDelegate <NSObject> -(void)hideMenu; @end @interface GOCategoryMenuViewController : UIViewController @property(weak, nonatomic) id <GOCategoryMenuViewControllerDelegate> delegate; @end 
  2. Implement hideMenu in the .m file

     -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.delegate hideMenu]; } 
  3. Import the header and delegate to the parentVC

     #import "GOCategoryMenuViewController.h" @interface ViewController : UIViewController<GOCategoryMenuViewControllerDelegate> 
  4. Implement the hideMenu in the .m of ParentVC

     -(void)hideMenu { NSLog(@"Hide the menu"); } 

I believe I need to also declare self on the delegate, but I don't know how and I tried searching for a solution on the web to no avail. It's why I'm asking directly now to you all. Please help and thanks!

If you want to solve it with delegates only, then you must pass the parent view controllers controls delegates to the child view controllers with declaring the properties that has the same type with the parent control in the child view controller.
Then you can do any operation on the parent view's control in the child view controller.

ParentViewController.h class

@interface ParentViewController : UIViewController
{
    NSMutableDictionary *dictOfPeople;
}

@property(nonatomic, retain) NSMutableDictionary *dictOfPeople;

ParentViewController.m class

@synthesize dictOfPeople;

- (void)anyMethod
{
    //initialize child view controller
    ChildViewController *childViewController = [[ChildViewController alloc] init];
    // here is important
    childViewController.owner = self; 
}

ChildViewController.h class

@interface ChildViewController : UIViewController
{
    id owner;
}

@property(nonatomic, assign) id owner;

ChildViewController.m class

@synthesize owner;

- (void)anyMethod
{
   // You can access parent view controllers dictOfPeople dictionary like this
   int totalNum = [((ParentViewController *)self.owner).dictOfPeople count];

   //...
}

Your protocols is true, only thing you have to do is when you init instance of GOCategoryMenuViewController you should give it a delegate sth like this:

GOCategoryMenuViewController *category = [[GOCategoryMenuViewController alloc] init];
//if you don't give delegate, never your delegate method will get called
category.delegate = self;

You have to delegate your instance of GOCategoryMenuViewController class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM