unexpected segue error found nil - swift

I get this error when pressing the home button: Thread 1: Fatal error: Unexpected Segue Identifier; nil
This is my code:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
switch(segue.identifier ?? "")
{
case "AddItem":
os_log("Adding a new day.", log: OSLog.default, type: .debug)
case "ShowDetail":
guard let newDayDetailController = segue.destination as? newDayController else {
fatalError("Unexpected destination: \(segue.destination)")
}
guard let selectednewDayCell = sender as? newDayTableViewCell else {
fatalError("Unexpected sender: \(String(describing: sender))")
}
guard let indexPath = tableView.indexPath(for: selectednewDayCell) else {
fatalError("The selected cell is not being displayed by the table")
}
let selectedDay = days[indexPath.row]
newDayDetailController.day = selectedDay
case "toMenu":
os_log("Back to main menu", log: OSLog.default, type: .debug)
if segue.identifier == "sendData"
{
let VC = segue.destination as! HomeViewController
VC.data = totalDays!
}
default:
fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))")
}
}
//MARK: Actions
#IBAction func menuButton(_ sender: Any) {
performSegue(withIdentifier: "sendData", sender: self)
}
Someone please help me out?

Make sure to set the segue identifier in the Attributes Inspector in your storyboard
Apparently your switch statement goes to the default case.

Related

if let editItemVC = segue.destination as? ItemDetailViewController {} doesn't work

i have two segues in this tableViewController, but the downcasting in prepare(for: segue) doesn't work.
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
print("into segue")
super.prepare(for: segue, sender: sender)
switch segue.identifier {
case "addItem":
print("addItem Segue")
if let addItemVC = segue.destination as? ItemDetailViewController {
print("if let done")
addItemVC.container = container
} else {
print("WTF")
}
case "editItem":
print("editItem Segue")
if let editItemVC = segue.destination as? ItemDetailViewController {
guard let selectedItemCell = sender as? PriorityListTVCell else {
fatalError("Unexpected sender: \(String(describing: sender))")
}
guard let indexPath = tableView.indexPath(for: selectedItemCell) else {
fatalError("The selected cell is not being displayed by the table")
}
editItemVC.editedInfo = list[indexPath.row]
print("passed indexPath.row: \(indexPath.row)")
editItemVC.container = container
}
default:
break
}
}
if i click "add" button, in the console it'll print out
into segue
addItem Segue
WTF
not sure why the downcasting doesn't work, does it have anything to do with segue types of destination?
Thanks for help!
This
if let addItemVC = segue.destination as? ItemDetailViewController {
won't work if the destination vc isn't of type ItemDetailViewController meaning you need to assign the class name in IB , for help you can do
print(type(of:segue.destination))
For navigation do
if let nav = segue.destination as? UINavigationController ,
let addItemVC = nav.viewControllers?.first as? ItemDetailViewController {}

Use the tag of UIButton in the prepare segue

I have a button that after tapping, perform a segue, in this perform, I send the tag of UIButton, with the sender
#IBAction func nextBtn(_ sender: UIButton) {
performSegue(withIdentifier: "more", sender: sender.tag)
}
and in the prepare segue, I try to use this tags, but appearntly i am doing it wrong
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "more" {
if let navigationVC = segue.destination as? UINavigationController, let openPDF = navigationVC.topViewController as? Morepage {
let senderTag = (sender as? UIButton)?.tag
if senderTag == 1 {
print(123)
} else if senderTag == 2 {
print(123)
}
}
}
}
The senderTag is shown as none or nil. could you help me to use sender.tag properly in the prepare segue.
many thanks
You can check your button tag following way. sender: will be sender not sender.tag
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "more" {
if let navigationVC = segue.destination as? UINavigationController, let openPDF = navigationVC.topViewController as? Morepage {
if yourButton.tag == 1 {
print(123)
} else if yourButton.tag == 2 {
print(123)
}
}
}
}
OR
if sender.tag == 1 {
print("Button 1 is pressed")
}

Data from parse is not being passed through prepareForSegue

I want to get the objectId from parse and pass it through segue. But the objectId is passing over as an empty string:
class QueryViewController: UIViewController {
var objectIdFormParse = String()
#IBAction func makeQueryButtonTapped(_ sender: UIButton) {
makeSearchObject.saveInBackground { (success, error) in
if error == nil {
if let getObjectId = makeSearchObject.objectId {
self.objectIdFormParse = getObjectId
print("objectId in queryVC: \(self.objectIdForomParse)")
}
//Successfully saved
} else {
//Error, check error
}
}
performSegue(withIdentifier: resultsSegue, sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == resultsSegue
{
let destination = segue.destination as! ResultsViewController
destination.objectIdFromQueryVC = objectIdForomParse
}
}
}
The print statement prints the objectId correctly, but the segue passes empty.
You can pass the data through perform segue. just change perform segue line with following one:
performSegue(withIdentifier: resultsSegue, sender: objectIdForomParse)
And in your prepare for segue method add following lines:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == resultsSegue
{
let destination = segue.destination as! ResultsViewController
let objectIDParse = sender as! String
destination.objectIdFromQueryVC = objectIDParse
}
}
}
Perform segue when your error is nil since you are setting objectIdForomParse
only when your error is nil.
Update :
#IBAction func makeQueryButtonTapped(_ sender: UIButton) {
makeSearchObject.saveInBackground { (success, error) in
if error == nil {
if let getObjectId = makeSearchObject.objectId {
self.objectIdFormParse = getObjectId
print("objectId in queryVC: \(self.objectIdForomParse)")
performSegue(withIdentifier: resultsSegue, sender: self)
}
//Successfully saved
} else {
//Error, check error
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == resultsSegue
{
let destination = segue.destination as! ResultsViewController
destination.objectIdFromQueryVC = objectIdForomParse
}
}
}

Swift one out of three segue is not passing data

I am trying to pass data to another controller with a segue after clicking on a button.
This is my code:
#IBAction func agilityDogBtnPressed(_ sender: Any) {
if let objs = controller.fetchedObjects, objs.count > 0 {
let course = objs[0]
self.performSegue(withIdentifier: "DogAgilitySegue", sender: course)
}
}
#IBAction func baseEducationBtnPressed(_ sender: Any) {
if let objs = controller.fetchedObjects, objs.count > 0 {
let course = objs[1]
self.performSegue(withIdentifier: "BaseEducationSegue", sender: course)
}
}
#IBAction func puppyBtnPressed(_ sender: Any) {
if let objs = controller.fetchedObjects, objs.count > 0 {
let course = objs[2]
self.performSegue(withIdentifier: "PuppyClassSegue", sender: course)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "DogAgilitySegue" {
if let destination = segue.destination as? ClassDetailsVC {
if let course = sender as? Course {
destination.course = course
}
}
} else if segue.identifier == "PuppyClassSegue" {
if let destination = segue.destination as? ClassDetailsVC {
if let course = sender as? Course {
destination.course = course
}
}
} else if segue.identifier == "BaseEducationSegue" {
if let destination = segue.destination as? ClassDetailsVC {
if let course = sender as? Course {
destination.course = course
}
}
}
My problem is that the first two segues are working perfectly fine.
The third one (PuppyClassSegue) is not passing the data through.
It opens the page but the data are not showing, basically the destination.course is not happening.
Any idea how is that possible?
Thank you!
It appears that course is nil , also you can do this by hooking all buttons to the same action and setting tags for them 0,1,2 respectively
#IBAction func agilityDogBtnPressed(_ sender: UIButton) {
if let objs = controller.fetchedObjects, objs.count > 0 {
let course = objs[sender.tag]
self.performSegue(withIdentifier: "SegueFromCon", sender: course)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ClassDetailsVC {
if let course = sender as? Course {
print("hit here ",course)
destination.course = course
}
}
}
make 1 segue named say SegueFromCon from the current vc to the detailsVC

Add two guard let in a prepare for segue function

I'm trying to build my first app with three tableViews which are hierarchical. The middle VC has two guard let in one prepare for segue function.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let destination = segue.destination as? AddMemberViewController else {
return
}
destination.club = club
guard let Destination = segue.destination as? TransactionViewController, let selectedRow = self.tableViewMember.indexPathForSelectedRow?.row else {
return
}
Destination.member = members[selectedRow]
}
That's how it looks like. Can I fix this, that both func get used by my app, because it just uses the one on the top.
The problem is if you want to segue to TransactionViewController the function already returns because segue.destination is not AddMemberViewController.
Instead you should give your segues different identifiers and ask for them in prepareForSegue. Something like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "AddMemberVCSegue" {
guard let destination = segue.destination as? AddMemberViewController else {
return
}
destination.club = club
}
if segue.identifier == "TransactionVCSegue" {
guard let Destination = segue.destination as? TransactionViewController, let selectedRow = self.tableViewMember.indexPathForSelectedRow?.row else {
return
}
Destination.member = members[selectedRow]
}
}
Just replace guards with if lets:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? AddMemberViewController {
destination.club = club
} else if let destination = segue.destination as? TransactionViewController, let selectedRow = self.tableViewMember.indexPathForSelectedRow?.row {
destination.member = members[selectedRow]
}
}
prepare(for is called for one specific segue, so executing both guard let after another is unnecessarily expensive.
Usually you are checking the identifier of the segue with a switch statement, replace the literal identifiers with your real values
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case "memberSegue":
let destination = segue.destination as! AddMemberViewController
destination.club = club
case "transactionSegue":
guard let selectedRow = self.tableViewMember.indexPathForSelectedRow?.row else { return }
let destination = segue.destination as! TransactionViewController
destination.member = members[selectedRow]
default: break
}
}