简体   繁体   中英

How to pass which function should call in next screen after tap on button based on flow without using enum in IOS Swift?

I am using MVVM Arch with RxSwift, I have one screen commonly used for more than 8 flows, Here I don't want to use enum and switch casses but I have to call different functions based on flow and route to different screens

Ex: I have Two classes A and B and both will go to C, Here C have one button with two functions Now once we navigate to C and I click button in C then it should call function related to A class and route to some other controller How can I do this without enums

Mean I have to decide in A or B classes which function should call in C after clicking on button

class ViewControllerA: UIViewController {
   override func viewDidLoad() {
    }

    @IBAction func btn_clicked(_ sender: Any) {

       let vc: ViewControllerC = 
       self.storyboard?.instantiateViewController(withIdentifier: 
        "ViewControllerC") as! ViewControllerC
       vc.isFrom = "A"
      self.navigationController?.pushViewController(vc, animated: true)
   }
 }

 class ViewControllerB: UIViewController {
    override func viewDidLoad() {
    }

    @IBAction func btn_clicked(_ sender: Any) {

       let vc: ViewControllerC = 
       self.storyboard?.instantiateViewController(withIdentifier: 
        "ViewControllerC") as! ViewControllerC
       vc.isFrom = "B"
      self.navigationController?.pushViewController(vc, animated: true)
   }
 }
class ViewControllerC: UIViewController {

    var isFrom = ""
    override func viewDidLoad() {
        super.viewDidLoad()
     if isFrom == "A"{
        self.callForA()
      }else if isFrom == "B"{
        self.callForB()     
    }
  }

  func callForA(){
       //Your Code
  }
  func callForB(){
       //Your Code
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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