简体   繁体   中英

swift how to push data to other view by dismissing the current viewcontroller?

I am trying to push some data back to the previous view controller ( Let's call this AViewController ) from my current one ( Let's call this BViewController )

however, when I use performSegue method it shows me the AViewController I wanted but makes the tab bar controller at the botton of the screen disappear (iirc this is because it creates a new "view" with the performSegue method?)

but when I use the dismiss method no data is passed back (neither in the completion with self.performSegue)

So how do I push my data back to AViewController while still have my tab bar controller at the bottom?

Here's my code

 // Push data to AViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    var aViewController = segue.destination as? AViewController
    if routineReps.isEmpty == false {
        for i in 0...routineReps.count-1 {
            routineViewController?.workOutNameArray.append(routineNames[i])
            routineViewController?.workOutSetsArray.append(routineSets[i])
            routineViewController?.workOutRepsArray.append(routineReps[i])
        }
    }
}

 // Button Action to push data back to previous viewcontroller
 @IBAction func completeAddingWorkoutButton(_ sender: UIButton) {

    self.performSegue(withIdentifier: "addWorkoutsForTodaySegue", sender: self)
 }

As mentioned you can use Delegates or NSNotificationCenter or Singleton Patterm

Scenario is you navigate from Class A to Class B and now want to pass data from Class B to A while you come back to Class A, you want to have that passed data

 Class A -> Navigates -> Class B
       <- Sends Data back

Delegate -

Define a protocol in Class B

Class BViewController

 @objc
 protocol ClassBDelegate {
      func passingSomeValue(val : String)
  }

  class BViewController{
        weak var delegate : ClassBDelegate! //define a variable of the protocol
       func passValueWhenFromThisMethod(){
             let someString = "this value will get passed"
              delegate.passingSomeValue(someString)
              self.navigationController?.popViewController(animated: true)
        }
  }

Class AViewController

  class AViewController{
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if let segue.identifier = "segueidentifierforgoingtoclassBVC"{
           var classBVC = segue.destinationViewController as! BViewController
             classBVC.delegate = self
       }
  }
   }

  extension AViewController : ClassBDelegate{
    func passingSomeValue(val : String){
         print("Got this value from Class B: \(val)")
     }
   }

use delegate to pass data to previous ViewController following the method given in link below:

Passing data back from view controllers Xcode

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