I'm new to iOS dev, I'm trying to switch between storyboards.
I followed the answer provided here 'Receiver (<ViewController>) has no segue with identifier 'addSegue'.
I did every step and put the below code in my viewDidLoad, because I want it to automatically go to a new storyboard, depending no a condition. Am I doing something wrong?
self.performSegue(withIdentifier: "identifierName", sender: self)
your code is write in prepareForSegue method
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "yourIdentifierInStoryboard")
{
self.performSegue(withIdentifier: "identifierName", sender: self)
}
}
Like Vacawama said, it's not possible to do it in the ViewDidLoad, so I've put it in the ViewDidAppear and it works just fine.
Related
In Xcode 13.1 I programmed the following code into my source view controller. It's working so well that it's calling the destination view controller twice (instead of once).
This is my code:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToNextViewControllerSegue" {
let destVC = segue.destination as! DestinationViewController
destVC.categoryTag = tag
}
}
Any idea what I'm doing wrong?
Let me include the IBAction code as well (This doesn't seem to be the problem, though.)
#IBAction func startButtonTapped(_ sender: UIButton) {
if tag == 1 || tag == 2 {
self.performSegue(withIdentifier: "goToNextViewControllerSegue", sender: nil)
} else {
createAlert()
}
}
Thanks for any help!
Add a breakpoint to prepare(for segue: sender:) and look at the backtrace to see where it is being called from. My guess is that you have connected the button to the segue in the storyboard, and then also to the action method mentioned in your question, so the segue is being performed twice.
I have been following this tutorial: http://www.totem.training/swift-ios-tips-tricks-tutorials-blog/ux-chops-dim-the-lights
However I have edited it slightly so that I can specify different Segues and also do it programmatically.
The problem occurs when I close the popped up view. When I close it, the background dim stays there:
What I did to the project files:
files: (https://github.com/TotemTraining/DimBackground.git)
1) Deleted the Segue that was there
2) Created an IBAction for the button named clickedButton
3) Created new Segue from first VC to second Named the Segue testSegue
4) Added this code for the IBAction:
#IBAction func clickedButton(sender: AnyObject) {
performSegueWithIdentifier("testSegue", sender: self)
}
5) Changed the prepareForSegue to:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "testSegue") {
dim(.In, alpha: dimLevel, speed: dimSpeed)
}
}
Now, When I run it, it shows the popup the desired way however when I click the close it removed it but leaves the "Dim" there. Can anyone see why?
The view is going to have several segues from it and I only want a few of them to have this "dim" effect.
Edit:
If I take out the if (segue.identifier == "testSegue") so its now:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//if (segue.identifier == "testSegue") {
dim(.In, alpha: dimLevel, speed: dimSpeed)
//}
}
it works as desired, so is it something to do with that?
It sounds like your unwind isn't getting called in order to dim out. Have you set a breakpoint to make sure? Also, if you are using Xcode 8, there was a weird bug with unwind segue names. They automatically got appended with "WithSegue:" at the end of them, so double check that it is labeled correctly in Interface Builder.
I have a button with the following code:
#IBAction func buttonPress(sender: AnyObject) {
performSegueWithIdentifier("newAccount", sender: sender)
}
When the button is pressed it performs a segue. I want to pass a value to the new view controller so I added the following code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "newAccount" {
if let dvc = segue.destinationViewController as? NewAccountViewController {
dvc.testValue = "This is my test value I want to pass"
}
}
}
The NewAccountViewController doesn't receive the value (I don't get any error).
The code I have in my NewAccountViewController is:
var testValue:String?
When I print(testValue) I don't get anything.
Why isn't this working as expected?
You need to check if the class is set correctly
Like the ViewController in below image
There are maybe two reasons: 1. identifier is not set as "newAccount" in the storyboard; 2. you have put a navigator view controller into the NewAccountViewController, which would ask you to transfer the destination as UINavigationController instead of NewAccountViewController. Hope this helps.
So I have multiple buttons on one view segue'ing to other views. I have the function prepareForSegue() at the bottom of my code preparing one segue, however the app crashes when I use another segue on the view, even if it doesn't need any preparing.I think that the problem is that all of the segues use the prepareForSegue() function, however the storyboard ID is different than the view it is transitioning to.Is there any way to specify a prepareForSegue() function for each separate segue on the same view?Code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let theVC: ViewController = segue.destinationViewController as! ViewController
theVC.receivedString = "true"
}
Thanks
In your prepareForSegue you should use a condition to check segue identifier like:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "yourSegueToAnyController")
{
//do code for specific viewController
}
}
I'm fairly new to swift so please take it easy on me. I'm trying to use two buttons to go back to the same View Controller. One button will have some logic in it and the other button will simply cancel the scene and go back. I would like to do this by using the same segue.
#IBAction func reduceButton(sender: AnyObject) {
performSegueWithIdentifier("BackToViewController", sender: sender)
}
#IBAction func cancelButton(sender: AnyObject) {
performSegueWithIdentifier("BackToViewController", sender: sender)
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "BackToViewController"{
let firstScene = segue.destinationViewController as! ViewController
firstScene.reducedString = reducedString
}
I am unsure if this is the correct way to do this or if I am doing it completely wrong. Neither of my buttons will work and I'm stumped. Any help would be greatly appreciated.