Pass data back to previous viewcontroller
You can use a delegate. So in your ViewController B you need to create a protocol that sends data back to your ViewController A. Your ViewController A would become a delegate of ViewController B.
If you are new to objective C, please look at What is Delegate.
Create protocol in ViewControllerB.h :
#import <UIKit/UIKit.h>
@protocol senddataProtocol <NSObject>
-(void)sendDataToA:(NSArray *)array; //I am thinking my data is NSArray, you can use another object for store your information.
@end
@interface ViewControllerB : UIViewController
@property(nonatomic,assign)id delegate;
ViewControllerB.m
@synthesize delegate;
-(void)viewWillDisappear:(BOOL)animated
{
[delegate sendDataToA:yourdata];
}
in your ViewControllerA : when you go to ViewControllerB
ViewControllerA *acontollerobject=[[ViewControllerA alloc] initWithNibName:@"ViewControllerA" bundle:nil];
acontollerobject.delegate=self; // protocol listener
[self.navigationController pushViewController:acontollerobject animated:YES];
and define your function:
-(void)sendDataToA:(NSArray *)array
{
// data will come here inside of ViewControllerA
}
Edited :
You can See this example : How you can Pass data back to previous viewcontroller: Tutorial link
Swift passing calculated data back to previous view controller
My suggestion is to use a callback closure. It's less code and easier to handle than protocol / delegate.
In AddSubtractMoney
declare a callback
variable and call it in sendBackUpdatedNumber
passing the Double
value
class AddSubtractMoney: UIViewController {
// ...
var callback : ((Double)->())?
// ...
func sendBackUpdatedNumber(){
let newestUpdate = UserInfo(whatYouSpentToday: runningNumber, oldDailyBudgetPassed: dailyBudgetPassedThrough)
amountPassedBackToUserInfo = dailyBudgetPassedThrough - Double(runningNumber)!
newestUpdate.goalToSaveDaily = amountPassedBackToUserInfo
print(amountPassedBackToUserInfo)
callback?(amountPassedBackToUserInfo)
}
}
In prepare(for segue
assign the closure to the callback
variable and add the code to be executed on return
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "addOrSubtractMoney"{
let addOrSubtractMoneyVC = segue.destination as! AddSubtractMoney
addOrSubtractMoneyVC.callback = { result in
print(result)
// do something with the result
}
addOrSubtractMoneyVC.dailyBudgetPassedThrough = userDailyBudgetPassedOver
}
}
Transferring data back to the root ViewController
If you are using segues, you could create an unwind segue and use it instead of popToRootViewController. Here you can see how to create an unwind segue.
Otherwise, based on your hierarchy, before popToRootViewController, you could access your view controller like this:
if let rootVC = navigationController?.viewControllers.first as? YourViewControllerClass {
rootVC.someProperty = dataToPass
}
navigationController?.popToRootViewController(animated: true)
Passing data from Side Menu back to its original View Controller
Instead of creating a new instance of CountryDetailsVC
in tableView(:didSelectRowAt:)
, you should store a reference to CountryDetailsVC
in your parent view controller in a variable, for example in prepare(for:sender:)
of the embed segue and then call your function on that variable later on.
Swift 3 - Passing data between a View Controller and after that to another 2
Before calling presentViewController
add :
nextViewController.name = yourTextField.text
You could also delete the segue
call. That is redundant.
Here is an example that I've used in the past :
@IBAction func doSegue(_ sender: UIButton) {
buttonTag = sender.tag
let storyboard = UIStoryboard (name: "Main", bundle: nil)
let resultVC = storyboard.instantiateViewController(withIdentifier: "ResultViewController")as! ResultViewController
// Communicate with new VC - These values are stored in the destination
// you can set any value stored in the destination VC here
resultVC.firstValue = buttonTag
resultVC.secondValue = randomOpponentValue()
self.navigationController?.pushViewController(resultVC, animated: true)
}
Swift, Pass data back from popover to view controller
You can use delegate(protocol) methods to send back data to previous view controller.
IN CURRENT VC:
protocol MyProtocol: class
{
func sendArrayToPreviousVC(myArray:[AnyObject])
}
Make a var in your class.
weak var mDelegate:MyProtocol?
Now call the protocol method when you pop the view controller, with your "properties" array as parameter.
mDelegate?.sendArrayToPreviousVC(properties)
IN PREVIOUS VC:
In your previous VC, set the mDelegate property to self, when you push the current VC.
currentVC.mDelegate = self
//PUSH VC
Now implement the protocol method in your previous VC.
func sendArrayToPreviousVC(myArray:[AnyObject]) {
//DO YOUR THING
}
Related Topics
Type Alias Declaration with Templates in Swift
iOS 10. Coredata Insert New Object Sig Abrt
How to Get The Advantages of Scenekit's Level Editor Programatically
Uisearchcontroller Hiding Status Bar on iOS 11 Swift 4
Generating a Simple Algebraic Expression in Swift
Syntax to Create Dictionary in Swift
Metal: Limit Mtlrendercommandencoder Texture Loading to Only Part of Texture
Swiftui Index Out of Range in Foreach
How to Custom The Image of Mkannotation Pin
Swift-Animate Cashapelayer Stroke Color
Sharing Screenshot of Swiftui View Causes Crash
Creating Decoration View as Custom Column in UIcollection View
Uislider Handle Changes Width on Different Size Ipads
When Creating Thread Safe Reads in Swift, Why Is a Variable Create Outside The Concurrent Queue