I am currently trying to segue to a different view controller when a button is pressed. The button's action method is called correctly, but when it calls the perform segue with identifier it crashes the app and says "terminating with uncaught exception of type NSException". I have the segue identifier as "switchToMap". I segue to the same page from a different place in the app as well, but that seems to work just fine. Does anyone have any ideas?
func mapButtonPressed (sender: UIButton) {
performSegueWithIdentifier("switchToMap", sender: sender)
}
The other place the map page is segued to looks like this. It is part of a switch statement.
case 2:
performSegueWithIdentifier("map", sender: indexPath.item)
Related
I have searched for a similar question to this but cannot find it because mine is very obscure.
I am currently developing a social network app. All my code seems to be correct. However, there is the share button that is not working. I will attach the code here:
#IBOutlet weak var shareBtn: UIButton!
which is in one of my files to connect it up and:
#objc func toCreatePost (_ sender: AnyObject) {
performSegue(withIdentifier: "toCreatePost", sender: nil)
}
Before you ask, all of my segues are connected, the buttons are connected to the code and the identifier is correct.
I don't know if it could be an error in Xcode itself or my code, but the button clicks when it is run however it does not do anything. Help would be appreciated.
The error is that when the button is clicked it does not do anything and will not go to the next ViewController even though the code seems to be correct.
#IBAction func toCreatePost (_ sender: AnyObject) {
performSegue(withIdentifier: "toCreatePost", sender: nil)
}
Above is the code with an IBaction which I edited which now gives me a crash. The crash is a Thread 1: signal SIGABRT
Here is my main.storyboard
See this for view-based issues click here
It seems you have not added navigation controller before the view controller from where you have to perform segua action.
if your problem still not resolved try to push manualy without segua
I have a segue from one view controller to another, which was created in the storyboard of my project. I'm wondering if there is a way to change or get rid of that segue through code. Thanks, using Xcode 9 Swift 4.
Apparently it's not possible... look at the answer on this post. And the documentation says this about creating segues:
"You do not create segue objects directly. Instead, the storyboard runtime creates them when it must perform a segue between two view controllers."
Although it's not about changing or deleting the segue, I think it's the same logic.
If you just want to avoid the segue to happen you can do that inside "shouldPerformSegue" (like vacawama said in the comments):
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if identifier == "aSegueIWantToDisable" {
return false
}
return true
}
func checkForRecipes(noRecords: Bool) {
//segue to addNewRecipe page
if noRecords == true{
print("Can't Find any Recipes!")
self.performSegue(withIdentifier: "ToAddNewRecipeVC", sender: self)
}else{
print("error, noRecords not equal to zero")
}
I am able to segue successfully via the storyboard but want to do so programmatically based on information returned from a delegate.
Upon running the app, the information from the delegate is successfully sent to the function "checkForRecipes" -i.e "noRecords" returns TRUE, but for some reason, the below line of code within that function does not seem to execute (and no errors are thrown):
self.performSegue(withIdentifier: "ToAddNewRecipeVC", sender: self)
The app starts up but stops at the main screen, whereas it should segue to the "AddNewRecipe" view controller.
The segue itself definitely has a segue ID of "ToAddNewRecipeVC". I have also tried dispatching to the main queue (to no avail) based on the following thread.
I'm stumped - what's going wrong here?
OK, it looks as though I have solved the problem. I embedded the main view controller into a navigation controller and now everything works as intended. I tried this same tactic earlier and it kept throwing up errors. grrr!
Anyway - thank you to all for the input!
I am trying to make a reset button for my app that will reset the UI to the original state. I have made a UIButton and linked it to the ViewController, but I have no idea where to go from here. I tried using the following code:
#IBAction func resetToOriginalState(sender: UIButton) {
self.resetToOriginalState (sender: UIButton)
}
It gave me the following error:
Editor placeholder in source file
Sorry if there may be an obvious answer, but I am very new to Swift and Xcode.
Is there any other way to create a reset button?
The error:
Editor placeholder in source file
Is because you are calling a function with the UIButton Class name instead of the actual button.
#IBAction func resetToOriginalState(sender: UIButton {
// this line is wrong, you shouldn't have UIButton in here
self.resetToOriginalState (sender: UIButton)
// the line should read
self.resetToOriginalState (sender: sender)
}
This way, you are passing the actual button into the function that was passed to resetToOriginalState
Seems you have too IBAction for the same button, check how many time you have #IBAction func resetToOriginalState(sender: UIButton) in your code and remove the references from the references Interface list to clean it, should there be only one :
It depends what is in the scene and what do you need to reload. As far is I know you can't really segue a ViewController to itself, but here are few options:
Try to add loadView() when the button is pressed
Duplicate the view controller, and segue between the two. (might be risky and create more work)
Reset your variables to their initial state when the button is pressed
You should give us more detail because this is implementation specific.
Nevertheless, it's not very clean, but depending on the architecture of your code, you might be able to generate a new instance of your view controller, destroy the current one, and present the new one.
I have created a local notification in Swift which gives the option to end a current game without having to go back in to the app. That's all fine and works as it should. The issue I'm having is that if the user does this, I don't want them to go back to the Game view controller if that happens to be the last view that was open when the app entered the background. I would like them to go back to the app's Home view controller instead.
I expected to be able to add a perform segue to my Game view controller in the following way, should the criteria match. I tried adding it to viewDidAppear(), but it didn't work:
override func viewDidAppear(animated: Bool) {
if isThereACurrentGame() == false {
performSegueWithIdentifier("unwindToHomeScreen", sender: self)
}
}
Is this something to do with viewDidAppear() not being called when the app comes back to the foreground? If so, what might an alternative be?
P.S. My isThereACurrentGame() function works as it should, as does the performSegueWithIdentifier elsewhere in the view controller, so these aren't the cause of the problem.