I want to get the objectId from parse and pass it through segue. But the objectId is passing over as an empty string:
class QueryViewController: UIViewController {
var objectIdFormParse = String()
#IBAction func makeQueryButtonTapped(_ sender: UIButton) {
makeSearchObject.saveInBackground { (success, error) in
if error == nil {
if let getObjectId = makeSearchObject.objectId {
self.objectIdFormParse = getObjectId
print("objectId in queryVC: \(self.objectIdForomParse)")
}
//Successfully saved
} else {
//Error, check error
}
}
performSegue(withIdentifier: resultsSegue, sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == resultsSegue
{
let destination = segue.destination as! ResultsViewController
destination.objectIdFromQueryVC = objectIdForomParse
}
}
}
The print statement prints the objectId correctly, but the segue passes empty.
You can pass the data through perform segue. just change perform segue line with following one:
performSegue(withIdentifier: resultsSegue, sender: objectIdForomParse)
And in your prepare for segue method add following lines:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == resultsSegue
{
let destination = segue.destination as! ResultsViewController
let objectIDParse = sender as! String
destination.objectIdFromQueryVC = objectIDParse
}
}
}
Perform segue when your error is nil since you are setting objectIdForomParse
only when your error is nil.
Update :
#IBAction func makeQueryButtonTapped(_ sender: UIButton) {
makeSearchObject.saveInBackground { (success, error) in
if error == nil {
if let getObjectId = makeSearchObject.objectId {
self.objectIdFormParse = getObjectId
print("objectId in queryVC: \(self.objectIdForomParse)")
performSegue(withIdentifier: resultsSegue, sender: self)
}
//Successfully saved
} else {
//Error, check error
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == resultsSegue
{
let destination = segue.destination as! ResultsViewController
destination.objectIdFromQueryVC = objectIdForomParse
}
}
}
Related
In a single button, i put both alart system and a pop up view. If the text fields are blank then it will send an alart. If everything is okay then it will show popup view. Alart system and pop up is conflicting. How do I set conditon to segue function when to perform or not?
The function is not written proper. I can understand. But I am confused writing it. How do i call the overrride func?
func stateCondition(state: Bool) {
if state {
print("Entered into the state") //entering into the func
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "info"{
_ = segue.destination as! InfoViewController
}
if segue.identifier == "popUp" {
let vc = segue.destination as! PopUpViewController
vc.age = Double(ageTextField.text!)!
vc.gender = genderTextField.text!
vc.bmi = Double(bmiLabel.text!)!
print("Entered into the segue") //is not entering into this func
}
}
}
}
Try to read the code and comments.
First of all, the method func prepare(for segue: UIStoryboardSegue, sender: Any?) must not be in another method. Somehow you managed to squeeze it in func stateCondition(state: Bool) . Second you are not calling performSegue(withIdentifier: identifier, sender: self) anywhere. You probably should :) Check out the code, hope it helps. I remember my first segue, it took me some time to understand what is going on.
class ViewController: UIViewController {
#IBOutlet private var ageTextField: UITextField!
#IBOutlet private var genderTextField: UITextField!
#IBOutlet private var bmiLabel: UILabel!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// 3
// This method gets called and there you do your stuff with respective VCs
if segue.identifier == "info", let infoViewController = segue.destination as? InfoViewController {
// 3.1
// If the identifer is set to INFO then you go to the InfoViewController and assigne message
infoViewController.message = "Some fields are empty"
} else if segue.identifier == "popUp", let popUpViewController = segue.destination as? PopUpViewController {
// 3.2
// If the identifer is set to POPUP then you go to PopUpViewController and assign age, gender and bmi
popUpViewController.age = "33"
popUpViewController.gender = "male"
popUpViewController.bmi = "20"
} else {
print("Identifer is none of the above")
}
}
#IBAction private func buttonTapped(_ sender: Any) {
// 1.
// First you need to figure out where you want to take the user
// You do that in the method getSegueIdentifier() where you get the identifier
let identifier = getSegueIdentifier()
// 2.
// Then you performSegue with that identifer
performSegue(withIdentifier: identifier, sender: self)
}
private func getSegueIdentifier() -> String {
if ageTextField.text?.isEmpty == true && genderTextField.text?.isEmpty == true && bmiLabel.text?.isEmpty == true {
return "info"
} else {
return "popUp"
}
}
}
class InfoViewController: UIViewController {
var message = ""
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
showAlert()
}
func showAlert() {
// show alert with message
print(message)
}
}
class PopUpViewController: UIViewController {
var age = ""
var gender = ""
var bmi = ""
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
showPopUp()
}
func showPopUp() {
// show popUp with age gender and bmi
print(age, gender, bmi)
}
}
There is general misunderstanding: You must not call prepare(for segue – besides the syntax is wrong anyway – the function is called by the framework just before the segue is performed.
You probably mean something like
func stateCondition(state: Bool) {
if state {
print("Entered into the state") //entering into the func
performSegue(withIdentifier: "info", sender: nil)
} else {
performSegue(withIdentifier: "popUp", sender: nil)
}
}
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popUp" {
let vc = segue.destination as! PopUpViewController
vc.age = Double(ageTextField.text!)!
vc.gender = genderTextField.text!
vc.bmi = Double(bmiLabel.text!)!
print("Entered into the segue") //is not entering into this func
}
}
here is my problem :
I would like to recover the variable jan from the Launch App() function and insert it in the override above instead of "Hello there".
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
let destVC : troisViewController = segue.destination as! troisViewController
destVC.dataFromFirst = "Hello there"
}
func launchApp(decodedURL: String) -> Void {
if presentedViewController != nil{
return
}
let jan: String = "\(decodedURL)"
print(jan)
self.performSegue(withIdentifier: "troissegue", sender: self)
}
The problem is the decoded URL is a barcode obtained by using the camera of my phone, a solution of type : destVC.dataFromFirst = launchApp() does not work...
Does Anyone has a similar issue ?
Thank you in advance
A simple solution is to pass the string as sender parameter
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "troissegue" {
let destVC = segue.destination as! troisViewController
destVC.dataFromFirst = sender as! String
}
}
func launchApp(decodedURL: String) -> Void {
if presentedViewController != nil { return }
self.performSegue(withIdentifier: "troissegue", sender: decodedURL)
}
I want to assign button tag to variable. Then I trying send this data to SecondViewController and assign it to var.
#IBAction func goToProducts(_ sender: UIButton) {
performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: self)
categoryNumber = sender.tag }
Pass data to ProductsViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToProducts" {
let destinationController = segue.destination as! ProductsViewController
destinationController.categoryNumb = categoryNumber
}
}
At ProductsViewController I create var but there isn't this data. I print this variable and I get this:
categoryNumber: 373
categoryNumb: Optional(24)
Can you tell me haw can I do this?
You need to assign it first before performSegue
categoryNumber = sender.tag
performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: self)
OR use sender
performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: sender.tag)
Pass data to ProductsViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToProducts" {
let destinationController = segue.destination as! ProductsViewController
destinationController.categoryNumb = sender as! Int
}
}
I have a button that after tapping, perform a segue, in this perform, I send the tag of UIButton, with the sender
#IBAction func nextBtn(_ sender: UIButton) {
performSegue(withIdentifier: "more", sender: sender.tag)
}
and in the prepare segue, I try to use this tags, but appearntly i am doing it wrong
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "more" {
if let navigationVC = segue.destination as? UINavigationController, let openPDF = navigationVC.topViewController as? Morepage {
let senderTag = (sender as? UIButton)?.tag
if senderTag == 1 {
print(123)
} else if senderTag == 2 {
print(123)
}
}
}
}
The senderTag is shown as none or nil. could you help me to use sender.tag properly in the prepare segue.
many thanks
You can check your button tag following way. sender: will be sender not sender.tag
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "more" {
if let navigationVC = segue.destination as? UINavigationController, let openPDF = navigationVC.topViewController as? Morepage {
if yourButton.tag == 1 {
print(123)
} else if yourButton.tag == 2 {
print(123)
}
}
}
}
OR
if sender.tag == 1 {
print("Button 1 is pressed")
}
I am trying to pass data to another controller with a segue after clicking on a button.
This is my code:
#IBAction func agilityDogBtnPressed(_ sender: Any) {
if let objs = controller.fetchedObjects, objs.count > 0 {
let course = objs[0]
self.performSegue(withIdentifier: "DogAgilitySegue", sender: course)
}
}
#IBAction func baseEducationBtnPressed(_ sender: Any) {
if let objs = controller.fetchedObjects, objs.count > 0 {
let course = objs[1]
self.performSegue(withIdentifier: "BaseEducationSegue", sender: course)
}
}
#IBAction func puppyBtnPressed(_ sender: Any) {
if let objs = controller.fetchedObjects, objs.count > 0 {
let course = objs[2]
self.performSegue(withIdentifier: "PuppyClassSegue", sender: course)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "DogAgilitySegue" {
if let destination = segue.destination as? ClassDetailsVC {
if let course = sender as? Course {
destination.course = course
}
}
} else if segue.identifier == "PuppyClassSegue" {
if let destination = segue.destination as? ClassDetailsVC {
if let course = sender as? Course {
destination.course = course
}
}
} else if segue.identifier == "BaseEducationSegue" {
if let destination = segue.destination as? ClassDetailsVC {
if let course = sender as? Course {
destination.course = course
}
}
}
My problem is that the first two segues are working perfectly fine.
The third one (PuppyClassSegue) is not passing the data through.
It opens the page but the data are not showing, basically the destination.course is not happening.
Any idea how is that possible?
Thank you!
It appears that course is nil , also you can do this by hooking all buttons to the same action and setting tags for them 0,1,2 respectively
#IBAction func agilityDogBtnPressed(_ sender: UIButton) {
if let objs = controller.fetchedObjects, objs.count > 0 {
let course = objs[sender.tag]
self.performSegue(withIdentifier: "SegueFromCon", sender: course)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ClassDetailsVC {
if let course = sender as? Course {
print("hit here ",course)
destination.course = course
}
}
}
make 1 segue named say SegueFromCon from the current vc to the detailsVC