0

I am adding child view controller to main view, child view controller contains two buttons that have their action methods defined in child view controller class. On the action of those buttons need to dismiss the child view controller. Is there any way to do so or better way to do.

Please guide. Thanks in advance.

Note: ChildViewController contains custom popup view, not using UIAlertController.

Update: Code in MainViewController

-(void)showAlertView
{   
    customAlertView = [[CustomAlertController alloc] init];
    [self displayContentController:customAlertView :msg :cancel :delete];
}

- (void) displayContentController: (UIViewController*) content: (NSString *) alertMsg: (NSString *) btnOneTitle: (NSString *) btnTwoTitle
{
[self addChildViewController:content];      // 1
content.view.bounds = self.view.bounds;     //2
content.strAlertMsg = alertMsg;
[content.view setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height)];
[self.view addSubview:content.view];
[content didMoveToParentViewController:self];          // 3
}

// ChildViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

self.lblMsg.text = strAlertMsg;
[self.btnCancel setTitle:strBtnTitle1 forState:UIControlStateNormal];
[self.btnDelete setTitle:strBtnTitle2 forState:UIControlStateNormal];

}

Question: How to pass data from parent to child view controller

iPhone Programmatically
  • 1,211
  • 2
  • 22
  • 54

3 Answers3

0

Make Custom XIB in which you implement your popup view.

Khawar Islam
  • 2,556
  • 2
  • 34
  • 56
0

Better than Child ViewController you can use another separate view controller for it.

1) First Thing Create One view controller like "ShowPopVC"

2) Defile typealias as per need in your view controller

typealias iSNeed = (Bool) -> Void
private var setResponsehandler: iSNeed?

3) add this method in that class and replace with your class name

class func show(in selectedViewController: UIViewController,handler: @escaping iSNeed) {
    let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ShowPopVC") as! ShowPopVC
    controller.modalPresentationStyle = .overCurrentContext
    selectedViewController.present(controller, animated: false, completion: {
        controller.setResponsehandler = handler
    })
}

4) Add your button actions and set return value

@IBAction func buttonYesNoClicked(_ sender: UIButton) {
    self.setResponsehandler!((sender.tag == 1) ? true:false)
    self.dismiss(animated: false, completion: nil)
}

-> and last you can open this view from anywhere from your application by using this simple method

@IBAction func buttonPopClicked(_ sender: UIButton) {
    ShowPopVC.show(in: self) { (iSNeed) in
        print(iSNeed)
    }
} 
0

Create a block called selfDismissBlock in the .h file of your child controller class

typedef void (^dismissBlock)(void);

@property (copy, nonatomic) dismissBlock selfDismissBlock;

Upon clicking the close button on your child controller class call the block.

self.selfDismissBlock();

First of all add your child view controller to your parent view as mentioned here.

for (UIViewController *childController in self.childViewControllers)
{
   if ([childController isKindOfClass:[YourChildController class]])
   {
     return;
   }
}

[self addChildViewController:self.yourChildControllerObj];
[self.yourChildControllerObj didMoveToParentViewController:self];

UIViewController * __weak weakViewController = self.yourChildControllerObj;
self.yourChildControllerObj.selfDismissBlock = ^{
       [weakViewController willMoveToParentViewController:nil];
       [weakViewController removeFromParentViewController];
       [weakViewController.view removeFromSuperview];
    };
Dinu
  • 49
  • 3