Hello i am facing the problem as the prepare for segue function is not working properly i've the code can you please tell me how can i debug it as it has optional conditions how i can remove them and check what's the problem or what i am doing wrong as i just want a class to be initialized in target vc before the app navigates to other screen.
#IBAction func filterBtnPressed(_ sender: Any) {
self.performSegue(withIdentifier: "FilterSessionVC", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "FilterSessionVC", let navigation = segue.destination as? UINavigationController, let vc = navigation.topViewController as? FilterSessionVC {
vc.presenter = FilterSessionPresenterImplementation()
}
}
Basic debugging...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let identifier = segue.identifier
print("identifier:", identifier)
let dest = segue.destination
print("destination:", dest)
if let navigation = dest as? UINavigationController {
print("dest IS a UINavigationController")
if let vc = navigation.topViewController as? FilterSessionVC {
print("Top Controller IS a FilterSessionVC")
// here you can proceed
vc.presenter = FilterSessionPresenterImplementation()
} else {
print("Top Controller IS NOT a FilterSessionVC")
}
} else {
print("dest IS NOT a UINavigationController")
}
}
Set a breakpoint on the first line, then step through and inspect the results.
I'm trying to pass 1 variable to another view controller and then another view controller.
var selectedPlace = Place {
name = Daniel Webster Highway;
country = United States;
lat = 42.72073329999999;
lon = -71.44301460000001;
}
When I select on a cell, I have access to a variable selectedPlace
I want to pass it on to my PlaceDetailVC and also MapVC.
For some reasons, I can't make that happen.
PlacesVC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! PlaceDetailVC
if let indexPath = tableView.indexPathForSelectedRow {
destinationVC.selectedPlace = (places?[indexPath.row])!
destinationVC.selectedTrip = selectedTrip!
}
}
PlaceDetailVC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toMap" {
let destinationVC = segue.destination as! MapVC
if let indexPath = placesTable.indexPathForSelectedRow {
destinationVC.selectedPlace = selectedPlace
}
}
}
I also try
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! MapVC
if let indexPath = placesTable.indexPathForSelectedRow {
destinationVC.selectedPlace = selectedPlace
}
}
MapVC
print(selectedPlace,"<<<<<<")
Result
I kept getting
Any hints on what I did wrong ?
No need to check indexPath if the segue "toMap" is called by the nav button
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toMap" {
let destinationVC = segue.destination as! MapVC
destinationVC.selectedPlace = selectedPlace
/*
if let indexPath = placesTable.indexPathForSelectedRow {
destinationVC.selectedPlace = selectedPlace
}*/
}
}
I have two controllers with UINavigationController (UIViewController(root) and UITableViewController). I need to click action at UIViewController and print something when move to UITableViewController
TableViewController.swift
func printString() {
print("hello world")
}
ViewController.swift
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "sendData" {
let navController = segue.destinationViewController as! UINavigationController
let controller = navController.topViewController as! TableViewController
controller.printString()
}
}
And after click I see that
Could not cast value of type 'myProjectName.TableViewController' (0x5a068) to 'UINavigationController' (0x17ffed4).
Why is that happening?
What about just:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "sendData",
let controller = segue.destinationViewController as? TableViewController {
controller.printString()
}
}
}
It looks like the destinationViewController is the TableViewController.
I’ve tried to push segue by using UICollectionView (show image form MasterView to DeatilView) but is not working. Xcode also doesn’t show any error message. What’s wrong with my code. I need some advice.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.collectionView?.indexPathForCell(sender as! UICollectionViewCell) {
let detailVC = segue.destinationViewController as! DetailMenuViewController
detailVC.picFood = self.collection[indexPath.row]
collection is empty array get data form json
[title = self.nameFood.title], it is working in DetailMenuViewController.swift but not image on code above.
You need to add the UICollecitonViewDelegate
func collectionView(collection: UICollectionView, selectedItemIndex: NSIndexPath)
{
self.performSegueWithIdentifier("showDetail", sender: self)
}
After that add
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.collectionView?.indexPathForCell(sender as! UICollectionViewCell) {
let detailVC = segue.destinationViewController as! DetailMenuViewController
detailVC.picFood = self.collection[indexPath.row]
}
}
}
I'm facing the error message:
"UIStoryboardSegue does not have a member named 'identifier'"
Here's the code causing the error
if (segue.identifier == "Load View") {
// pass data to next view
}
On Obj-C it's fine using like this:
if ([segue.identifier isEqualToString:#"Load View"]) {
// pass data to next view
}
What am I doing wrong?
This seems to be due to a problem in the UITableViewController subclass template. It comes with a version of the prepareForSegue method that would require you to unwrap the segue.
Replace your current prepareForSegue function with:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "Load View") {
// pass data to next view
}
}
This version implicitly unwraps the parameters, so you should be fine.
Swift 4, Swift 3
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "MySegueId" {
if let nextViewController = segue.destination as? NextViewController {
nextViewController.valueOfxyz = "XYZ" //Or pass any values
nextViewController.valueOf123 = 123
}
}
}
I think the problem is you have to use the ! to unbundle identifier
I have
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
if segue!.identifier == "Details" {
let viewController:ViewController = segue!.destinationViewController as ViewController
let indexPath = self.tableView.indexPathForSelectedRow()
viewController.pinCode = self.exams[indexPath.row]
}
}
My understanding is that without the ! you just get a true or false value
For Swift 2.3,swift3,and swift4:
Create a perform Segue at didSelectRowAtindexPath
For Ex:
self.performSegue(withIdentifier: "uiView", sender: self)
After that Create a prepareforSegue function to catch the Destination segue and pass the value:
Ex:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "uiView"{
let destView = segue.destination as! WebViewController
let indexpath = self.newsTableView.indexPathForSelectedRow
let indexurl = tableDatalist[(indexpath?.row)!].link
destView.UrlRec = indexurl
//let url =
}
}
You need to create a variable named UrlRec in Destination ViewController
Swift 1.2
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "ShowDeal") {
if let viewController: DealLandingViewController = segue.destinationViewController as? DealLandingViewController {
viewController.dealEntry = deal
}
}
}
Prepare for Segue in Swift 4.2 and Swift 5.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "OrderVC") {
// pass data to next view
let viewController = segue.destination as? MyOrderDetailsVC
viewController!.OrderData = self.MyorderArray[selectedIndex]
}
}
How to Call segue On specific Event(Like Button Click etc):
performSegue(withIdentifier: "OrderVC", sender: self)
this is one of the ways you can use this function, it is when you want access a variable of another class and change the output based on that variable.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let something = segue.destination as! someViewController
something.aVariable = anotherVariable
}
Provided you aren't using the same destination view controller with different identifiers, the code can be more concise than the other solutions (and avoids the as! in some of the other answers):
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
if let myViewController = segue.destinationController as? MyViewController {
// Set up the VC
}
}
Change the segue identifier in the right panel in the section with an id. icon to match the string you used in your conditional.
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
if(segue!.identifier){
var name = segue!.identifier;
if (name.compare("Load View") == 0){
}
}
}
You can't compare the the identifier with == you have to use the compare() method