Trying to pass data to container view with segue not working - ios

I am trying to pass a users id to a container view by preparing for a segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "childSegue" {
if let destinationVC = self.childViewControllers.first as? InterestCollectionController {
destinationVC.currentUser = self.currentUser
}
}
}
however, this is not working. I have my storyboard that looks like this.
storyboard I have also tried using segue.destination as! InterestCollectionController and that did not work either. From my understanding this is the way I should be passing data to my container view. Any help is appreciated thanks!

Just try this without using if statement:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "childSegue" {
let destinationVC = segue.destinationViewController as InterestCollectionController
destinationVC.currentUser = self.currentUser
}
}
1. Make sure your segue identifier matches with storyboard identifier.
2. Also verify once you have given exact segue to the above mentioned controller in storyboard.

Try this one
var destinationVC : InterestCollectionController?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "childSegue") {
self.destinationVC = segue.destination as? InterestCollectionController
destinationVC.currentUser = self.currentUser
}
}

I tested that the view of the embedded view controller is not loaded, in other words, the viewDidLoad() was not invoked, before the containerVC's prepare() was called.
Therefore, you cannot directly pass data to the destination view controller inside the prepare() function.

Related

prepare function cause crash

In my swift app I've the following prepare function:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? LocalPictureCVC {
destination.selectedLocal = selectedLocal
}
}
But when I try to use selectedLocal in LocalPictureCVC it crash due to selectedLocal is nil.
How can I solve this problem?
Try this I hope it will help you:-
first make sure to give segue properly with identifier then code like below
then in first ViewController
var valueToPassInSecondVC = ""
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//NOTE:- make sure about your identifier is correct as well as destination ViewController
if segue.identifier == "yourIdentifierOfSegue" {
let destVC = segue.destination as! ContainerViewController
destVC.valueFromFirstVC = self.valueToPassInSecondVC
}
}
in your destination class
var valueFromFirstVC:String = ""
//In my case pass String from FirstViewController to SecondViewController
thanks,I hope it will help you.
try checking your segue identifier in your prepare function
if segue.identifier == "Something"

Use of unresolved identifier 'segue'

I'm trying to export my data from a ViewController to another with this code:
func prepare(for: UIStoryboardSegue, sender: AnyObject?){
let desty : EndViewController = segue.destinationViewController as! EndViewController
desty.totalScore = scoreTotal.text!
}
But when I try to do that is turns me the error:
[Use of unresolved identifier 'segue']
But I've already done it as you can see here:image
So how can I solve it.
I will be so grateful if you help me.
Please, help a young programmer to learn.
First, perform your segues as below:
self.performSegue(withIdentifier: "YourSegueID", sender: self)
Now, call prepareForSegue and change the values in destination accordingly:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "YourSegueID" {
let vc = segue.destination
vc.yourVariable = "Some Value"
}
}
You are using the wrong function. It should be...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
}
}
The internal parameter label segue is missing in the first parameter and the type of the second parameter is wrong.
Please use code completion
Comment out your entire method
// func prepare(for: UIStoryboardSegue, sender: AnyObject?){
// let desty : EndViewController = segue.destinationViewController as! EndViewController
// desty.totalScore = scoreTotal.text!
// }
Type prep, you will see
Choose the proper line and press return.
Copy the body of the former method into the new one.

Pass data from tableview to view controller in tab bar in Swift

Im fairly new to Xcode and seems to be having a bit of a problem. I was passing data from my tableviewcontroller to EventViewController and it worked fine with the code bellow:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? EventViewController {
destination.Event = events[(tableView.indexPathForSelectedRow?.row)!]
}
}
But I needed to make some changes to the app and Embedded a tab bar controller to the EventViewController, and now I can't get it to run. How do I pass data to the First tab in the Tabbarcontroller instead? :) Thank you very much for your help.
Try this
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = (segue.destination as? UITabBarController)?.viewControllers.first as? EventViewController {
destination.event = events[(tableView.indexPathForSelectedRow?.row)!]
}
}

Transfer data from segue to segue with different navigation controller

I want to transfer data from the view on the bottom right to the one on the upper right. Thanks in advance.. :)
I think that what you search are unwind segues.
You can find a good how to about segues in this article from Apple:
https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html
Especially interesting for you should be the section "Creating an Unwind Segue".
The result should then be similar to this:
1. Give your segue a identifier, "make sure the identifier should be created from Controller"
2. use this function
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourIdentifier" {
let dvc = segue.destination as! yourVC
`// pass data
dvc.data = data
}
}
You can use it's override method to pass data
1) apply segue.identifier in storyBoard
2) In Action add self.performSegue(withIdentifier:"addTask" , sender: indexPath)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "addTask" {
let taskData:Task?
let vc = segue.destination as! AddTaskVC
vc.taskData = taskList[indexpath.row]
vc.isUpdateTask = isEditTask
}
}
You can use this when you set segue from first controller to third. Do check for identifier names while creating segue.
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "yourIdentifier1"
{
let nextScene = segue.destination as? UIViewController
nextScene.stringUserName = "Mike"
}
else if segue.identifier == "yourIdentifier2"
{
let laterScene = segue.destination as? UIViewController
laterScene.count = 5
}
}
If you still find and error, please show the error

Segue destination returns SWRevealViewController instead of UIViewController

I am preparing the values from func prepare. The problem is the application crashes because the destination returns SWRevealViewController instead of UIViewController. I am using SWRevealViewController on the storyboard.
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if (segue.identifier == "Main") {
let view = (segue.destination as! Main)
view.expired_login = self.expired_login
}
}
It crashes on the let view line. Here is the message
Could not cast value of type 'SWRevealViewController' (0x10afa72a8) to 'JetWayTrade.Main' (0x10afac038).
Is there a way to get the Main so that I can set its properties? Here is the storyboard image
If I understand you correctly you will want to cast the destination view controller as SWRevealViewController:
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if (segue.identifier == "Main") {
let view = (segue.destination as! SWRevealViewController)
view.expired_login = self.expired_login
}
}
If you are expecting your Main view controller to be the destination then you will have to check that your segue names are set correctly in your storyboard.

Resources