Prepare for segue function using SW revealer - ios

I'm trying to send a bool through a SWRevealViewControllerSeguePushController using prepareForSegue function, but it doesn't even gives me a response.
This code is in my side menu viewController (sw_rear). I've also set an identifier on the segue and this is my code so far:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "registerSegue" {
if let loginVC = segue.destination as? LoginVC {
print("Set it as true")
loginVC.registerActivated = true
}
} else {
if let loginVC = segue.destination as? LoginVC {
print("set it as false")
loginVC.registerActivated = false
}
}
}
I'm not receiving any output to the console with this code. Is there another good way to send data between ViewControllers using SWRevealViewControllerSeguePushController on a segue?
Thanks!

Related

PersonalityQuiz guided app in swift fundamentals

I've got problem with some additional challenges. I need to filter an array of type Question by some property and then pass it into next View Controller via segue. I've done this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let sender = sender as? UIButton else {return}
if sender == quiz3Button {
let vc = segue.destination as? QuestionViewController
vc?.correctQuestions = questions.filter { question in
return question.quiz == .animals
}
} else if sender == quiz4Button {
let vc = segue.destination as? QuestionViewController
vc?.correctQuestions = questions.filter { question in
return question.quiz == .cars
}
}
}
#IBAction func quiz3ButtonTapped(_ sender: UIButton) {
performSegue(withIdentifier: "animals", sender: sender)
}
#IBAction func quiz4Button(_ sender: UIButton) {
performSegue(withIdentifier: "cars", sender: sender)
}
Filtration works but it doesn't pass value to next View Controller. I declared variable in QuestionViewControler like that
var correctQuestions: [Question] = []
But when I need to access it I get error "Index out of range". So I figured that its empty..
Segues been made from buttons to VC
Ok. I've got it. The NavigationController was the problem here. Added into function push through NC and it worked ;) so closed I think
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let sender = sender as? UIButton else {return}
if sender == quiz3Button {
let destinationViewController = segue.destination as? UINavigationController
let questionViewController = destinationViewController?.viewControllers.first as! QuestionViewController
questionViewController.correctQuestions = questions.filter { questions in
return questions.quiz == .animals
}
} else if sender == quiz4Button {
let destinationViewController = segue.destination as? UINavigationController
let questionViewController = destinationViewController?.viewControllers.first as! QuestionViewController
questionViewController.correctQuestions = questions.filter { questions in
return questions.quiz == .cars
}
}
}

prepareForSegue called before performSegue

I am trying to perform a segue that passes a number of variables to the next view including one variable, currentID, which is retrieved from a parse database. performSegue should not be called until after currentID has been set to the currentID downloaded from the database. However, when I run the code, currentID ends up being an empty string when it is passed to the next view.
Here is my code called by the Button:
#IBAction func submitButtonPressed(_ sender: Any) {
let point = PFGeoPoint(latitude:0.0, longitude:0.0)
let testObject = PFObject(className: "Person")
testObject["inputAmount"] = inputAmount
testObject["outputAmount"] = outputAmount
testObject["inputCurrency"] = inputCurrency
testObject["outputCurrency"] = outputCurrency
testObject["location"] = point
testObject.saveInBackground { (success, error) -> Void in
// added test for success 11th July 2016
if success {
print("Object has been saved.")
self.currentID = String(describing: testObject.objectId!)
if(self.currentID != ""){
self.performSegue(withIdentifier: "mainToListSegue", sender: self)
}
} else {
if error != nil {
print (error)
} else {
print ("Error")
}
}
}
}
And here is the prepareForSegue method:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let listViewController = (segue.destination as! UINavigationController).viewControllers[0] as! ListViewController
listViewController.inputCurrency = inputCurrency
listViewController.outputCurrency = outputCurrency
listViewController.inputAmount = inputAmount
listViewController.outputAmount = outputAmount
listViewController.currentID = currentID
listViewController.cellContent = cellContent
}
To achieve your needs, you MUST connect your segue between viewcontrollers, and not from UIButton to viewcontroller.
Every time you need to prepare your segue before calling it, this is the procedure:
Then, name it and use delegate method
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mySegue" {
}
}
For navigating from one controller to another, connect your segue from view controller instead of from the button and it will work.

Can't seem to get a certain value to pass to another controller

So I have 3 controllers, let's say ChatVC, MenuVC, and InviteVC.
Starting on ChatVC, I open the menu and tap a button that dismisses the menu back to ChatVC, then segues to InviteVC.
Both ChatVC and MenuVC have the value I need to pass to InviteVC (currentRoomID). However it seems to be quite the issue getting that to InviteVC. currentRoomID is initialized on InviteVC as an empty string.
This is the action that is performed when I tap the button in the menu to take me to InviteVC:
#IBAction func invitePressed(_ sender: Any) {
weak var pvc = self.presentingViewController
self.dismiss(animated: true) {
pvc?.performSegue(withIdentifier: "chatInvite", sender: nil)
}
}
I've tried adding this in the dismiss closure, as well as in viewDidLoad of both MenuVC and ChatVC:
let inviteVC = InviteVipViewController()
inviteVC.currentRoomId = self.currentRoomID
I've tried passing it in ChatVCs prepareForSegue:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationViewController = segue.destination as? MenuViewController {
destinationViewController.transitioningDelegate = self
destinationViewController.interactor = interactor
destinationViewController.currentRoomID = self.currentRoomID
} else if segue.identifier == "chatInvite" {
let inviteVC = InviteVipViewController()
inviteVC.currentRoomID = self.currentRoomID
}
}
And every time, currentUserID remains an empty string when I get to InviteVC. It didn't seem to be a problem when I was segueing to InviteVC straight from MenuVC, but since I changed it to the current transition (menu drops away back to ChatVC, then segues to InviteVC), it's seeming to be impossible to get that value passed.
This is incredibly frustrating so if anyone can help me try something I haven't tried before it will be very much appreciated!
you have problem in prepareForSegue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationViewController = segue.destination as? MenuViewController {
destinationViewController.transitioningDelegate = self
destinationViewController.interactor = interactor
destinationViewController.currentRoomID = self.currentRoomID
// if identifier equals to chatInvite then you get your InviteViewController from segues Destination not by creating one
} else if segue.identifier == "chatInvite" {
let inviteVC = segue.destination as? InviteVipViewController
inviteVC.currentRoomID = self.currentRoomID
}
}
NOTE: What I can see your trying to segue from a ViewController which you have already dismissed. So I think you have to move that logic to your MainViewController in your case I assume it's MenuViewController
also you should not instantiate your InviteVIPViewController like this
let inviteVC = InviteVipViewController()
inviteVC.currentRoomId = self.currentRoomID
segue will do it for you
You can pass your currentRoomID using UserDefaults as well like
#IBAction func invitePressed(_ sender: Any) {
let userDefaults = UserDefaults.standard
userDefaults.set(currentRoomID, forKey: "roomID")
// call the synchronise so it sync force the user defaults to save
userDefaults.synchronize()
weak var pvc = self.presentingViewController
self.dismiss(animated: true) {
pvc?.performSegue(withIdentifier: "chatInvite", sender: nil)
}
}
Now access your currentRoomID in inviteViewController viewDidLoad()
also I assume the roomID as an int
let userDefaults = UserDefaults.standard
currentRoomID = userDefaults.integer(forKey: "roomID")
// if string then you can use object and cast it to string like this
currentRoomID = userDefaults.object(forKey: "roomID") as! String
This line:
let inviteVC = InviteVipViewController()
creates a completely new instance of the InviteVipViewController, completely unrelated to the instance to which you are about to segue. You should instead use segue.destination (which IS the instance you are about to segue to). The only proviso is that you should test to confirm that the destination is of the correct type (as you do for the segue to MenuViewController):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationViewController = segue.destination as? MenuViewController {
destinationViewController.transitioningDelegate = self
destinationViewController.interactor = interactor
destinationViewController.currentRoomID = self.currentRoomID
} else if segue.identifier == "chatInvite" {
if let inviteVC = segue.destination as? InviteVipViewController {
inviteVC.currentRoomID = self.currentRoomID
}
}
}
I used to use segues and found them unnecessarily complicated. Here's a workaround...
In the part of your code where you import UIKit (at the top), add a variable for whatever you want to pass, in this case, let's say I want to pass a score.
//view controller one
import UIKit
var score = 25
Then go in the view controller where you want to receive the-in this case score, and you can easily access it like it's a variable in your own view controller.
//Viewcontrollertwo
super.viewDidLoad()
score += 2
//score is now = 27, requires no segue

2-Way protocol oriented data passing in swift

I am trying to pass data back and forth between two viewControllers to maintain the data when segueing. I have a mainMenuVC and a SettingsVC. The settings are chosen inside the SettingsVC and then passed to the mainMenuVC to be stored, however, it seems that this does not happen and I am not sure why.
This is the code in mainMenuVC which conforms to the transferSettings protocol.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "optionsSegue" {
let receivingVC:SettingsVC = segue.destination as! SettingsVC
receivingVC.settingsDelegate = self
self.settingsDelegate = receivingVC
if self.settingsDelegate != nil{
if self.currentSettings == nil {
print("currentNil")
self.currentSettings = GameOptions()
}
self.currentSettings.description()
self.settingsDelegate.storeGameSettings(settings: self.currentSettings)
}else{ print("settingsDelegate in MainMenuVC nil")}
}
And this is the code inside `SettingsVC' which also conforms to the protocol above.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "settingsReturn" {
let recievingVC:MainMenuVC = segue.destination as! MainMenuVC
///Set the delegate of MainMenu to SettingsVC
recievingVC.settingsDelegate = self
if self.settingsDelegate != nil {
if self.ScrollView.settingsSelected != nil{
self.ScrollView.saveUserSettings()
print("settingsVC")
self.ScrollView.settingsSelected.description()
self.settingsDelegate.storeGameSettings(settings: self.ScrollView.settingsSelected)
}else{ print("No stored settings in the SettingsVC")}
}else{print("SettingsDelegate in SettingsVC is nil")}
}
}
The way I assumed this would work is that my data would simply pushed back and forth between the two viewControllers, but after examining with breakpoints, I found that after prepareforsegue is finished executing, the properties that store the data in the recievingVC are nil, thereby reseting the process.
Am I missing something here?
Thanks!

segue and transfer data from viewcontroller to viewcontroller to another viewcontroller

sorry for confusing title, but here is my problem. i do a segue from "superVC" (collectionviewcell) to "childVC" and its working well. then i want to segue again from that "childVC" to the "secondChildVC" (which is all the data is from superVC). is it possible? cause i always get a nil value when performing that segue.
its something like this : SuperVC -> ChildVC -> secondChildVC
here is the superVC segue:
var destination = [DestinationData]()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "DestinationDetailVC"{
let detailVC = segue.destination as? DestinationDetailVC
if let data = sender as? DestinationData{
detailVC?.destinationDetail = data
}
}
}
here is 2nd vc segue
var destinationDetail : DestinationData?
#IBAction func goToMapOnPressed(_ sender: Any) {
let detail = destinationDetail
self.performSegue(withIdentifier: "DestinationMapVC", sender: detail )
print(detail)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "DestinationMapVC"{
let detailVC = segue.destination as? DestinationMapVC
if let data = sender as? DestinationData{
detailVC?.destinationMapDetail = data
}
}
}
Thanks
I think you are sending array of DestinationData to first segue, but inside the first segue you have put condition, so that if the data would be of DestinationData kind class, then the data will be sent to nextVC, But in actual you are sending array of DestinationData object so the if condition fails and hence the data are not passed to childVC
That's why you might be getting nil in the secondChildVC as there is no data in destinationDetail of childVC.
Hence you can modify the condition to check for array as the destination holds array type of data. or else remove the condition from the prepareSegue method.
Code :
//Write this code in to get that clicked object then pass that object in sender
let destTemp = sender[your selected index]
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "DestinationDetailVC"{
let detailVC = segue.destination as? DestinationDetailVC
if let data = sender as? DestinationData{
detailVC?.destinationDetail = data
}
}
}

Resources