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.
Related
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.
I have 1 UIViewController that contains a UIContainerView and a UIButton.
Also, I have a UITableViewController (It has a UITextField and a UITextView) that is embedded in the UIContainerView in the UIViewController.
I'm trying to get the string values that will be available in TextField and in TextView.
I tried to use the segue to get the properties values, but I failed to do so, see the code below.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "TVCSegue" {
let containerTVC = segue.destination as? FieldsTVC
self.textField.text = FieldsTVC?.textField?.text
self.textView.text = FieldsTVC?.textView?.text
}
}
The code above has 'textField' and 'textView' as properties in the UIViewController to assign the values to.
But I believe that it doesn't work as I get the values before they changes. Please provide me with a functional way to do so.
When your main view loads, the container performs a segue to its assigned initial / root ViewController. At that point, you can get a reference to it:
var theFieldsTVC: FieldsTVC?
Now, in prepare for segue, assign that variable:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "TVCSegue" {
if let vc = segue.destination as? FieldsTVC {
theFieldsTVC = vc
}
}
}
Then, you can:
#IBAction func buttonTapped(_ sender: Any) {
self.textField.text = theFieldsTVC?.textField?.text
self.textView.text = theFieldsTVC?.textView?.text
}
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.
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.
I have two View Controllers. in mainViewController there is TabBarButton when it clicked user goes to 1stViewController and there is UIButton when it has clicked user goes to 2ndViewController.
before making my 2ndViewController everything worked fine. but after i add segue function in my program i got error.
could not cast value of type 'Calculator.1stViewController' (0x791a8) to 'Calculator.2ndViewController' (0x794c8).
briefly my code is
#IBAction func CalculateGpa(){
//Calucation Goes Here
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var AnotherName : 2ndViewController = segue.destinationViewController as! 2ndViewController
//Code here
}
UIButton Perform the segue and BarButton do nothing it just go to 1stView Controller. when i run the program and click BarButton im getting that above error.
its because prepareForSegue cant identify to which button it should perform right? how to solve it?
try this code.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "youridentifiername") {
//your class name
if let viewController: DealLandingViewController = segue.destinationViewController as? DealLandingViewController {
viewController.dealEntry = deal
}
}
}