How to override the prepareForSegue with multiple Segue's - ios

So I'm learning Core Data and I managed to create a segue with a parent relationship that allows me to segue to show only certain items based on the category you select in the previous VC (A Category View Controller). Now I have created a separate VC after this which shows the weather based on your location and to get to it you have to click the button from the original VC (The Category View Controller) to get to the weatherViewController. However every time I try to do so and perform a segue I run into this error below :
This is the code I have so far :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! NoteTableViewController
let weatherDestinationVC = segue.destination as! WeatherViewController
if let indexLocale = noteTableView.indexPathForSelectedRow {
destinationVC.selectionCategory = noteArray[indexLocale.row]
print(indexLocale.row)
} else {
print("Error")
weatherDestinationVC.delegate = self
performSegue(withIdentifier: "goToWeather", sender: self)
}
}
#IBAction func goToWeatherView(_ sender: UIBarButtonItem) {
performSegue(withIdentifier: "goToWeather", sender: self)
}
Any help would be appreciated since I am just learning Core Data!

Conditionally cast the different segue destination view controllers or check for the segue's identifier whichever you prefer.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destination as? NoteTableViewController {
if let indexLocale = noteTableView.indexPathForSelectedRow {
destinationVC.selectionCategory = noteArray[indexLocale.row]
print(indexLocale.row)
}
} else if let weatherDestinationVC = segue.destination as? WeatherViewController {
weatherDestinationVC.delegate = self
}
}
Also, you don't have to performSegue within prepareForSegue.

Related

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

Trying to pass data to container view with segue not working

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.

How to segue multiple view controllers programmatically using swift 3.0

I'm getting an error:
has no segue with identifier 'dest1''
and what I did was:
route(ctrl+drag) in storyboard viewcontroller -> dest1, viewcontroller -> dest2
have an action button in viewController.swift
set storyboard ids to "dest1" and "dest2"
#IBAction func Parameters(_ sender: AnyObject) {
self.performSegue(withIdentifier: "dest1", sender: nil)
}
Prepare for segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("0")
if segue.identifier == "dest1" {
if let svc = segue.destination as? secondViewController {
print("1")
}
} else if segue.identifier == "dest2" {
if let svc = segue.destination as? reverbViewController {
print("2")
}
}
What am I missing?
From your description, you have set storyboard ids to "dest1" and "dest2", whereas you need to set segue identifiers. Click on the segue itself in IB, and set it's property.
Storyboard identifiers are used when creating the view controller using code...
instantiateViewControllerWithIdentifier("myViewController")

Pass data from UIViewController to UISpiltView

Right now I am facing some problem to pass data from UIViewController to UISpiltViewController.
My app start with UIViewController, and after user login it, there is a segue from UIViewController to UISpiltViewController, UISpiltViewController is root view of a NavigationViewController, and this NavigationViewController is a root view of a TableViewController. I have written a class for this TableViewController, called MasterView, and right now I want to pass some information from loginView to this MasterView.
self.performSegueWithIdentifier("login", sender: self)
But I want to know how to write the prepareforsegue function.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "login"
{
let controller = (segue.destinationViewController as UINavigationController).topViewController as MasterViewController
controller.authorityNum = self.authorityArray
}
}
I want to pass an array, but I will get class cast error. Please help me with this.
Try this i am not sure but i think your segue is Model segue so
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "login"
{
var splitView:UISplitViewController = segue.destinationViewController as! UISplitViewController
var controller = splitView.viewControllers.first?.topViewController as! MasterViewController
controller.authorityNum = self.authorityArray
}
}
Try to NSLog every line it may help
Like this,
let destinationViewController = segue.destinationViewController as! MasterViewController
Swift 3 of the accepted answer.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "login" {
let splitView:UISplitViewController = segue.destination as! UISplitViewController
let navController = splitView.viewControllers.first as! UINavigationController
let controller = navController?.topViewController as! MasterViewController
controller.authorityNum = self.authorityArray
}
}

Resources