繁体   English   中英

如何从另一个视图 controller 触发/调用 function?

[英]How to trigger/call a function from another view controller?

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

deleteCartItemCell() function:-

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 视图 controller:

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
}

我试图通过创建 object 从 ConfirmPopUpVC 调用 deleteCartItemCell() function 但遇到此错误: 在此处输入图像描述

您可以使用闭包调用 function。

1 - 在ConfirmPopUpVC 中创建完成块

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

2 - 当你呈现视图 controller 时实现它

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

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

completion?()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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