Conditional Segue in Swift - ios

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.

Related

Conditional Segue while passing data?

I'm trying to make the segue from viewcontroller to 2ndviewcontroller only when my condition is met. I've connected the segue from the button in viewcontroller to the 2ndviewcontroller. So far I have:
#IBAction func switchView(_ sender: AnyObject) {
// if a and b's textfields aren't empty
if(!(a.text?.isEmpty)! && !(b.text?.isEmpty)!) {
a1 = a.text!;
b1 = b.text!;
}else{
// do something
}
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if(identifier == "2ndviewcontroller") {
if(!(a.text?.isEmpty)! && !(b.text?.isEmpty)!) {
return true
}
}
return false
}
With this, I've been able to make the segue ONLY when a and b's textfields are not empty. That's exactly what I want but I also want to pass data from viewcontroller to 2ndviewcontroller as well.
func prepForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "2ndviewcontroller" {
let 2ndviewcontroller = segue.destination as! 2ndviewcontroller
2ndviewcontroller.c = a
}
}
}
I used the code above to pass data from viewcontroller to 2ndviewcontroller. Problem is I don't know how to combine them to both pass data, AND only make the segue when condition is met. When I have both functions, the bool function executes correctly, but prepForSegue does not pass data. When I comment out the bool function, prepForSegue passes the data, but I'm cannot apply a condition for making the segue.
Edit: fixed by using prepareForSegue method provided in the comments below.
As discussed in the comments, the method name should be prepareForSegue, not prepForSegue.

segue getting twise call in swift

I have two view Controller one is ScheduleController and other is EditShiftController. In ScheduleController I have a table view name is scheduleTable. In the scheduleTable I have a cell with identifier ScheduleWithData. In this cell i have two different button Button EDIT and Button DELETE. i have directly connected button EDIT with EditShiftController with segue identifier editShiftSegue.
The Code of TableViewCell.swift
var editShiftClick: (() -> Void)? = nil
#IBAction func editShiftBtn(sender: AnyObject) {
if let onButtonTapped = self.editShiftClick {
onButtonTapped()
}
}
The Code of ScheduleController.swift
myCell!.editShiftClick = {
self.performSegueWithIdentifier("editShiftSegue", sender: dataTemp[indexPath.row].id)
}
The function for prepareSegue in ScheduleController.swift
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "editShiftSegue") {
print("senderIndexPath=\(sender)")
if (sender != nil) {
let svc = segue.destinationViewController as! EditShiftViewController;
svc.label = String(sender)
}
}
}
Snip Shot of Segue
enter image description here
When I click on EDIT button then EditShiftController getting twice call I don't know why
please, can any one help me here Thank you in Advance
myCell!.editShiftClick = {
//self.performSegueWithIdentifier("editShiftSegue", sender: dataTemp[indexPath.row].id)
}
Comment self.performSegueWithIdentifier("editShiftSegue", sender: dataTemp[indexPath.row].id) this line because if you have connected edit button directly with segue then no need to performsegue programatically also!!
Update:
myCell!.editShiftClick = {
self.performSegueWithIdentifier("editShiftSegue", sender: self)
}
don't comment that line and delete that segue which is given from edit button. give segue from viewcontroller to viewcontroller and give identifier editShiftSegue and then perform segue as above code.

How can i create a segue that pass data to another view controller only if certain criteria are met

I want to create a segue to pass data to another view controller but there are certain criteria that must happen for the segue to happen. If possible i would prefer to use the segue Id instead of the dragging method.
this is an example Im trying to accomplish
#IBAction func SubmitButtonPressed(sender: AnyObject) {
if 1<0 {
// dont perform segue
}else{
//Perform segue
// i want to pass this data in the next VC
var data = "foo"
//this is my segue id i want o use to go to the Second VC
var segueId = "segueForgotPasswordTwo"
// second VC
var secondVc = "viewController2"
// Iwant to to use prepare for segue but im getting errors in the parameters
prepareForSegue(UIStoryboardSegue, sender: AnyObject?){
}
}
}
Your question is a bit unclear but I believe this is what you are looking for...
func someFunction(){
if //some condition {
//code
}else if //some condition {
//code
} else {
//perform segue by using the folowing line. Assign the identifier to the segue in the storyboard.
//Do this by first creating a segue by dragging from a view controller to the destination view controller. Be sure to drag from the VIEWCONTROLLER, to the destination VIEWCONTROLLER. DO NOT just drag from the button. Next, choose the type of segue (eg. show or present modally), and then type in an indentifier for this segue.
performSegueWithIdentifier("SegueIdentifier", sender: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SegueIdentifier" {
//now find your view controller that you are seguing to.
let controller = segue.destinationViewController as! SomeViewController
//access the properties of your viewController and set them as desired. this is how you will pass data along
controller.property = someValue
}
}
Overview:
Hook the segue from the source view controller to the destination view controller (see left side red arrows)
Don’t hook it from the button to the destination view controller
Create an action for the button to do your custom condition check then perform segue
Screenshot:
Code:
var data = "foo"
#IBAction func buttonPressed(sender: UIButton) {
let someCondition = true
if someCondition {
performSegueWithIdentifier("showGreen", sender: self)
}
else {
performSegueWithIdentifier("showPink", sender: self)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showGreen" {
let greenVC = segue.destinationViewController as! GreenViewController
// Make sure the data variable exists in GreenViewController
greenVC.data = data
}
}
You can implement the shouldPerformSegueWithIdentifier function in your ViewController. When the segue is triggered, this function can cancel the segue if it returns false, so you can simply include whatever logic is required in this function and return true/false as appropriate.

iOS Segue not working as expected when passing value

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.

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

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

Resources