添加ChildViewController并在ChildViewController中执行按钮操作

时间:2018-01-03 11:07:33

标签: ios objective-c custom-view childviewcontroller

我将子视图控制器添加到主视图,子视图控制器包含两个按钮,这些按钮在子视图控制器类中定义了它们的操作方法。在那些按钮的动作上需要关闭子视图控制器。有没有办法这样做或更好的方法。

请指导。 提前谢谢。

注意:ChildViewController包含自定义弹出视图,而不是使用UIAlertController。

更新: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];

}

问题:如何将数据从父视图传递到子视图控制器

3 个答案:

答案 0 :(得分:0)

制作自定义XIB,您可以在其中实现弹出视图。

答案 1 :(得分:0)

比Child ViewController更好,您可以使用另一个单独的视图控制器。

1)First Thing创建一个视图控制器,如“ShowPopVC”

2)根据需要在视图控制器中定义typealias

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

3)在该类中添加此方法并替换为您的类名

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)添加按钮操作并设置返回值

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

- >最后,您可以使用这个简单的方法从应用程序的任何位置打开此视图

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

答案 2 :(得分:0)

在子控制器类的.h文件中创建一个名为selfDismissBlock的块

typedef void (^dismissBlock)(void);

@property (copy, nonatomic) dismissBlock selfDismissBlock;

单击子控制器类上的关闭按钮后,调用该块。

self.selfDismissBlock();

首先将您的子视图控制器添加到父视图中,如此处所述。

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];
    };
相关问题