如何从另一个视图控制器触发/调用函数?

时间:2021-02-23 12:16:44

标签: ios swift

我在 CartVC 视图控制器中有一个名为 deleteCartItemCell() 的函数。从 CartVC,我展示了一个名为 ConfirmPopUpVC 的视图控制器。我的问题是如何从 ConfirmPopUpVC 调用/触发 deleteCartItemCell() 函数。

deleteCartItemCell() 函数:-

func deleteCartItemCell(row:Int){
    cartVM.removeCart(itemType: cartList[row].itemType, itemId: cartList[row].itemId)
        .subscribe(onSuccess: { (response) in
            self.toast(response.message)
            if response.status{
                self.observerCartResponse()
                self.tblCartList.reloadData()
            }
        }) { (error) in
            self.toast(error.localizedDescription)
        }
        .disposed(by: cartVM.disposeBag)
}

以这种方式呈现 ConfirmPopUpVC 视图控制器:

func customPresent<T>(storyBoardIdentifier: String = "Main",animate: Bool = true, attacher: (T) -> Void = { _ in  } ) -> T where T: UIViewController{
    let destVc: T
    destVc = instantiateViewController(storyBoardIdentifier: storyBoardIdentifier)
    destVc.modalPresentationStyle = .overCurrentContext
    destVc.modalTransitionStyle = .coverVertical
    attacher(destVc)
    self.tabBarController?.present(destVc, animated: true, completion: nil)
    return destVc
}

我尝试通过创建对象从 ConfirmPopUpVC 调用 deleteCartItemCell() 函数,但遇到此错误: enter image description here

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

您可以使用闭包调用该函数。

1 - 在 ConfirmPopUpVC

中创建完成块

var completion: (()->Void)? = nil

2 - 当你呈现视图控制器时实现它

vc.completion = { // write your code here }

3 - 在执行代码时从 ConfirmPopUpVC 调用它

completion?()