No Segue with Identifier Error in Swift? - swift

I am trying to use the performSegueWithIndentifier function to to create a segue from one ViewController to the next. But, when I press the button with the UITApGestureRecognizer connected to it, the View shifts to the debugger panel.
Here is the error it is displaying:
ContaminateTargetViewController: has no segue with identifier 'showMasterChemistryViewController'
(I cut out the personal Info)
Here is the ViewControllers Class:
import Foundation
import UIKit
class ContaminateTargetViewController: UIViewController {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showMasterChemistryViewController" {
let chemistryMasterViewController = segue.destinationViewController as! ChemistryMasterViewController
}
}
#IBAction func showPlaylistDetail(sender: AnyObject) {
performSegueWithIdentifier("showMasterChemistryViewController", sender: sender)
}
}
I also previously had to manual segues from the 2 buttons I have on the ViewController and recently deleted them to switch over to the UITapGestureRecognizer for the convenience. I am wondering if I have an error in my code that I do not see or if previously deleting the manual segues from the View is causing this error. If the problem is rooting from the previously deleted manual segues please tell me how to fix this in your error. If the problem is rooting form the code, please leave the code I should add, delete, or substitute.
Any suggestions or input is greatly appreciated.
Thanks in advance.

This error is called when you do not have the requested segue connected to your view controller. Are you sure that you have the two view controllers connected via a segue named "showMasterChemistryViewController" on your storyboard?

Related

Unknown class QuoteDetailViewController in Interface Builder file with Xcode 12

I saw a lot of posts about this error in Xcode but all of them are quite old. I'm learning how to code in Swift and I'm following the instruction of book.
My Xcode is version 12.5.1. The goal of this app is to display a list of quotes (string) and when I tap on one of them, display a new view that has as a title the quote.
I have a very simple storyboard. There is a TableView with a segue to a View. I created a new Cocoa file for tableview. TableView is working.
I connect the TableView with the View.
I repeated the some process for the View. I added a new Cocoa file called QuoteDetailViewController, click on top of the view and in the Class I selected from the list the file.
When I run the application in the iPhone 11 simulator, the view doesn't have any quote as title. I put a breakpoint in the code connected to the TableView and the app stops there. If I put a breakpoint in the QuoteDetailViewController nothing happens but I see an error in the output
2021-08-11 19:13:17.215204+0100 QuoteApp[80238:4055489] [Storyboard] Unknown class QuoteDetailViewController in Interface Builder file.
The QuoteDetailViewController is very basic.
import UIKit
class QuoteDetailViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let quoteViewController = segue.destination as? QuoteDetailViewController {
if let selectedQuote = sender as? String {
quoteViewController.title = selectedQuote
}
}
}
}
I tried to build again and again the app. I tried to Clean Build Folder and build again. I tried to recreate the View and the code. After all of that, nothing changed. The error is still there.
The source code is on GitHub.
Update
If I click on the arrow at the end of Class and just before the dropdown, Xcode opens the file I expect to be opened.
Select the QuoteDetailViewController, open Side Inspector view and Update the Target Membership same as QuoteTableViewController
Select the controller On the Storyboard, and Add it to the module.
Check Screenshots for reference:
So, I said that in the QuoteDetailViewController I have this code.
import UIKit
class QuoteDetailViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let quoteViewController = segue.destination as? QuoteDetailViewController {
if let selectedQuote = sender as? String {
quoteViewController.title = selectedQuote
}
}
}
}
The error was that the function prepare must be in the QuoteTableViewController.

Perform UnwindSegue correctly

I am trying to create an Unwind segue that brings from the DetailViewcontroller to the SecondViewcontroller.
I have created an IBAction like this one that can be intercepted by Segues :
#IBAction func segueToSecondVC(sender: UIStoryboardSegue) {
print("segue intercepted by IBAction segueToSecondVC")
}
An placed it in the receiver ViewController (secondViewController).
Then I moved to the Detailviewcontroller and clicked to the third figure on the top of the viewcontroller (exit),clicked to the segueToSecondVC IBAction that appeared in the list of events and dragged it into the button "TURN BACK"(placed in the Detailviewcontroller) in order to associate the button to the event.
In this way the button should be connected with the event and should turn back to the secondVC with an automatic Hidden Segue.
But it doesn't work, the simulator crash and I am redirected to xCode editor with an error in the Appdelegate that says "Thread 1: signal SIGABRT"
I don't know why it doesn't work, anyone could help me?
you can use Unwind like this:
#IBAction func prepareForUnwind(segue: UIStoryboardSegue){}
#IBAction func closePressed(_ sender: Any) {
performSegue(withIdentifier: "unwindToAnotherVC", sender: nil)
}
good rule, is to add all your segues names to constants file:
Create swift file
import Foundation
// Segues
let TO_LOGIN = "toLogin"
let TO_CREATE_ACCOUNT = "toCreateAccount"
let UNWIND = "unwindToChannel"
and then use UNWIND in
#IBAction func closePressed(_ sender: Any) {
performSegue(withIdentifier: UNWIND, sender: nil)
}

Data between view controllers not passing

I am creating an application in which there are 6 view controller in storyboard. The thing is that data is shared between the default view controller and the first one ( say A and B) which i added. i am using the prepareforseque method for passing data. the problem started when i added two more view controller. lets say C and D i created two new swift files and changed the two view controller class name. i created a textbox and button in C and label in D. when i pressed the button, the value of the text field is not passing into the D view controller although i used the same methods and code which i used for A and B. do i have to do anything else when i want to pass data between two newly added view controller.
first viewcontroller in which when a button is pressed value 1 needed to be passed:
class PlaySelectMenu: UIViewController {
var value = Int()
#IBAction func twotofive(sender: AnyObject) {
value = 1
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let nextView : PlayGameView = segue.destinationViewController as! PlayGameView
nextView.x = value
}
}
the second view controller which receive the value and print it
import Foundation
import UIKit
class PlayGameView: UIViewController{
var x = Int()
override func viewDidLoad() {
super.viewDidLoad()
print(x)
}
}
here i have added both the view controller from the object library and not working with the default one which is present in storyboard by default. i dont know why these two viewcontroller are not working. please help.
Regards Dev
One solution would be to write the data out to NSUserDefaults and then read it back from NSUserDefaults in the other view controller. Probably not the proper or correct way to share data between two view controllers, but it's been a reliable work around for me.
Other than that, you'd need to share your code so that we can see what's occurring.
Can you post also the code in your controllers C & D. And also if you have copy/paste the code inside your first two controllers into the two others, are you sure that in your prepareForSegue method you have changed the name of the destination segue ?
Assuming you have created the segue in Storyboard:
All you need is to do is put all of needed updates in prepareForSegue because twotofive is called after prepareForSegue.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
value = 1
let nextView : PlayGameView = segue.destinationViewController as! PlayGameView
nextView.x = value
}
Since you have connected your segue from button click to view controller, when you press button segue is automatically called. Instead of connecting segue from button to VC, connect VC to VC. Then in button click method at the last add below line:
#IBAction func twotofive(sender: AnyObject) {
value = 1
self.performSegueWithIdentifier("<Name of the segue identifier>", sender: self)
}
This will call your prepareForSegue. If you are calling more then one VC using segue from a VC then you can use segue.identifier to check which VC was called as below
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier == "CVC" {
}

(Swift/XCODE) Creating multiple segues from a single table view controller

I am new to programming in Swift and I having the hardest time trying to figure out how to connect a static table view with 6 cells to 6 separate dynamic prototype views.
From looking at previous responses, I know that I have to identify each segue ("showRestaurant") and I thought it would go in the override segue method but I am not sure. I am also unsure of how to properly code it.
Please let me know if you have any solutions
Thanks so much
Diamond
With a static tableView you can simply ctrl-drag from the tableViewCell to the destination viewController. Choose the type of the segue and give it an identifier (i.e. segueToVC1).
Repeat that for each tableViewCell which should be linked to a viewController.
Then in your custom tableView class file you override prepareForSegue to do additional setup, passing values to the destination viewController etc:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueToVC1" {
// ...
}
}

App crashes on segue after embedding view controllers

I have a segue that works fine when it is directly from a UIViewController to a UITableViewController, but when I embed the second view in a navigation controller, the segue no longer seems to work and I get unexpectedly found nil while unwrapping optional error. I don't believe I need to change the code. Should I rebuild the app somehow? I've read about errors in Xcode 6 beta (though I'm using 6.1) that cause the order in which a developer builds the app to have an effect.
Because destination of your segue is not as NavigationController. You must set the controller as NavagationController first.
Try this code
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "showItemSegue") {
let controller: Your_TableViewController = (segue.destinationViewController as UINavigationController).topViewController as Your_TableViewController
}
}
I hope this work