1

I have a function named deleteCartItemCell() in CartVC view controller. From CartVC i am presenting a view controller named ConfirmPopUpVC. My question is how can i call/trigger deleteCartItemCell() function from ConfirmPopUpVC.

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)
}

Presenting the ConfirmPopUpVC view controller this way:

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
}

I tried to call deleteCartItemCell() function from ConfirmPopUpVC by creating an object but encountered this error: enter image description here

As If Prince
  • 257
  • 2
  • 10
  • Does this answer your question? [Delegates in swift?](https://stackoverflow.com/questions/24099230/delegates-in-swift) – mahan Feb 23 '21 at 12:27
  • https://learnappmaking.com/delegation-swift-how-to/ Read this too. – mahan Feb 23 '21 at 12:27
  • I tried delegation too but it is showing this error: - Property 'delegate' with type 'delegateFromCartVC.Protocol' cannot override a property with type 'AppDelegate' – As If Prince Feb 23 '21 at 12:34
  • Use a different name. Include your code if it does not work. – mahan Feb 23 '21 at 13:04

2 Answers2

1
SalDev
  • 398
  • 3
  • 9
1

You can call the function using closure.

1 - Create completion block into ConfirmPopUpVC

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

2 - implement it when you present view controller

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

3 - call it from the ConfirmPopUpVC when you execute code

completion?()