Unwind Segue using performSegueWithIdentifier - Doesn't work - Swift 2.1 - ios

I am trying to unwind in my button click, after receiving response.
LoginController
#IBAction func SignInPressed(sender: AnyObject) {
if (onSuccess) {
performSegueWithIdentifier("unwindToGlobal", sender: AnyObject?())
}
}
and this is where it comes from (GlobalController).
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if (segue.identifier == "globalToLogin")
{
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
let loginController = segue.destinationViewController as? LoginController
})
}
}
This one works and as soon as I enter the app (GlobalController is the starting view), I get directed to Login Page. However, when I click on the login button on LoginController, unfortunately it doesn't unwind.
When I try to remove these connections and ctrl + drag button to exit, it just doesn't do anything.
I couldn't find what I am doing wrong, thus it's not working. What am I missing?

Here a fews of articles about Unwind, that was super for me, maybe that can help
Unwind

Related

prepare segue - destination View Controller is called twice

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.

Incorrect data passed on first click of button

Salutations,
My problem is as follows, I have an app I am working on which contains two views. The first simply has 2 buttons labeled "Slide Show One", and "Slide Show Two".
When I click on the first button, it displays the information for the second slideshow as (due to my supreme novice-ness), I select which of the slideshows to select via a boolean as follows:
var button : Bool = false;
then:
#IBAction func slideShowOne() {
button = true;
}
#IBAction func slideShowTwo() {
print("clicked button 2");
button = false;
}
Finally in the prepareForSegue:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let passedPic = segue.destination as! ViewControllerTwo;
if(button == true) {
print(button);
passedPic.picsChosen = sLS1;
}
if(button == false) {
print(button);
passedPic.picsChosen = sLS2;
}
}
Only way to have it display the correct information is by clicking on say button 1 for btn1 slideshow, going back, then clicking on button 1 again. Why is this, does it have anything to do with how swift handles function calls?
Aside: Swift knowledge is now a grand total of one week.
EDIT: Got it working, much appreciated. Now a quick question, at this time my two IBActions are empty, but required since they have segues attached to them, would be the best way to either make them meaningful, or perhaps still be able to segue; I assume the prepare function is an absolute must, otherwise there is no way (of which I know), to send the required data to my second VC.
#IBAction func slideShowOne() {
//self.performSegue(withIdentifier: "slideOne", sender: self);
}
#IBAction func slideShowTwo() {
//self.performSegue(withIdentifier: "slideTwo", sender: self);
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
let passedPic = segue.destination as! ViewControllerTwo;
if(segue.identifier == "slideOne") {
passedPic.picsChosen = sLS1;
} else {
passedPic.picsChosen = sLS2;
}
}
Try connecting view controller to view controller via storyboard - same ctrl dragging but whole view controller to other.
Give that segue identifier -> select identifier and on forth tab give it identfiier.
After that in one of your buttons, eg:
#IBAction func slideShowOne() {
button = true;
self.performSegue(withIdentifier: "toTheOtherVC", sender: self)
}
And you will have desired behaviour.
The reason is that: override func prepare(for segue: UIStoryboardSegue, sender: Any?) always be executed firstly.
So the actual workflow is as following:
-> 1) Initialize first view
-> 2) var button = false
-> 3) the user taps btn1
-> 4) func prepare is called
-> 5) second view is displayed (var button is still false at this moment, that's why you get incorrect response)
-> 6) func slideShowOne() is called (the user already is in second view)
-> 7) var button = true caused by func slideShowOne() (the user already is in second view)
-> 8) the user goes back
-> 9) the user taps btn1 again
-> 10) func prepare is called
-> 11) second view is displayed (the user gets correct response because value of var button has been changed in step7)
Let me know if you have any other questions. Have fun. : )

Multiple buttons use same segue in swift

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.

Navigation Between Two View Controller

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
}
}
}

Conditional Segue in Swift

I want to do a "connexion" button in my app. And when you tap on it, it will open a new page if you have correct login.
But, I read that we can't undo a segue, I suppose I need to call it manually. Do you have an idea ?
I still tried something like this :
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject) {
if checkLog == true
{
//finish
}
else
{
// undo
}
}
#IBAction func ConexTAP(sender: UIButton)
{
//check login
}
You can do the credentials check in the following method in your ViewController:
func shouldPerformSegueWithIdentifier(_ identifier: String!,
sender: AnyObject!) -> Bool
In alternative, you can bind the UIButton to an action in your view controller, perform the checks here and then trigger a segue by code.
In Swift you can proceed by:
Step 1: Insert a segue from ViewControllerA to ViewControllerB. The segue should start from ViewControllerA itself and not from any control (like Button).
Step 2: Give segue an unique identifier in storyboard. Ex: seageFromAtoB
Step 3 : In your ViewControllerforA.swift write :
if(condition == true) {
self.performSegueWithIdentifier("seageFromAtoB", sender: self)
}
If you are performing some task in some other Thread then using performSegueWithIdentifier can throw an exception.
If you get this error then you should use :
if(condition==true){
NSOperationQueue.mainQueue().addOperationWithBlock {
self.performSegueWithIdentifier("seageFromAtoB", sender: self)
}
}
This will perform segue from ViewControllerA to ViewControllerB with a specific condition.

Resources