jo3birdtalk
jo3birdtalk

Reputation: 616

Pass data with segue without displaying screen

I'm trying to achieve passing of data using segue, but without displaying the screen. There are two view controllers in my project and I will name them FirstViewController and SecondViewController.

On FirstViewController, I am using Show Adaptive Segue to pass data using performSegueWithIdentifier. The prepareForSegue method will be called, and recognising the identifier I've set to display SecondViewController.

However, the problem is I do not want to display SecondViewController. There are other things that my users may want to do before heading for SecondViewController.

I'm new to iOS programming, and I only know passing of data with segue. Please do share with me if there are methods to pass data apart from segue.

EDIT: To further elaborate my question. I'm working with TabBarController, and both View Controllers are accessible on the Tab bar. So when I am on SecondViewController with some data already "segue-ed" over from FirstViewController, I can head back to FirstViewController to add more data. At SecondViewController with UIRefreshControl I need the updated data.

Upvotes: 0

Views: 98

Answers (3)

Aliisa Roe
Aliisa Roe

Reputation: 146

From your user's perspective, they would not know whether the data is being updated before they segue or not, so it is probably fine to just pass the data with the prepareFoSegue function.

Otherwise, you could try setting a notification to the viewDidLoad on your FirstViewControllerand SecondViewController by

NSNotificationCenter.defaultCenter().addObserver(self, selector: nil, name: updateSecondVCNotification, object: nil)

and posting the notification from the FirstViewController whenever the data needs to be updated using the

NSNotificationCenter.defaultCenter().postNotificationName(updateSecondVCNotification, object: self)

Then on second view controller set the the selector of the addObserver to a string of the name of whatever function you want it to do like "reloadNewData"

func reloadNewData() { tableView.reloadData() }

Does this help? Don't forget to set the updateSecondVCNotification as a global string constant at the top of your FirstViewController. Learn more about NSNotificationCenter at https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/

Woo! Notifications! \(^▽^)/

Upvotes: 1

66o
66o

Reputation: 756

If you do the segue in code you can do what you want in a following way

1) Instantiate view controller from the story board

let storyboard: UIStoryboard = UIStoryboard(name: "SecondViewController", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("set_it_in_IB") as! SecondViewController

2) Set the properties you need to set as the view controller is already instantiated

vc.someProperty = "asd"

3) Segue!

viewController.presentViewController(viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?)

Hope that helps!

Upvotes: 1

Dimitre Bogdanov
Dimitre Bogdanov

Reputation: 396

I'm not sure if that would work for you, but you could simply keep the data in memory of your FirstViewController for the time during which the user is still interacting and giving feedback and once it's done, send all the data to the SecondViewController through the segue.

Upvotes: 0

Related Questions