moving data from Model to destination ViewController with segue
moving data from Model to destination ViewController with segue
I am new to swift and programming in general. I am building a quiz app. The app uses TopicsViewController to select a topic and segue to a QuestionsViewController. The questions for the various topics are stored as separate swift Objects file. I would like to pick the Topic1 Question file when I press the topic1 button in TopicsViewController to segue into the QuestionsViewController. I would like to know how can I select the particular questions file QuestionBank1/QuestionBank2 when I select the particular topic upon segueing to the QuestionsViewController?
Navigation Pane
Main.Storyboard
import UIKit
class TopicsViewController: UIViewController, returnToTopicVCDelegate
func goToTopicVC()
override func viewDidLoad()
super.viewDidLoad()
@IBAction func goToQuestionsVCWhenPressed(_ sender: UIButton)
performSegue(withIdentifier: "segueToQuestionVC", sender: self)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if segue.identifier == "segueToQuestionVC"
let quizVC = segue.destination as! QuizViewController
quizVC.delegate = self
import UIKit
import QuartzCore
protocol returnToTopicVCDelegate
func goToTopicVC()
class QuizViewController: UIViewController
var delegate : returnToTopicVCDelegate?
//outlet for the question label and image view
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var questionImageView: UIImageView!
//outlet for the buttons
@IBOutlet weak var optionAButton: UIButton!
@IBOutlet weak var optionBButton: UIButton!
@IBOutlet weak var optionCButton: UIButton!
@IBOutlet weak var optionDButton: UIButton!
@IBOutlet weak var optionEButton: UIButton!
//outlets for the progress
@IBOutlet weak var questionCounter: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
var allQuestions = QuestionBank()
var selectedAnswer : Int = 0 // answer selected by the subject
var questionNumber: Int = 0
var score: Int = 0
// functions executed after an answer is picked
@IBAction func answerPressed(_ sender: UIButton)
if sender.tag == selectedAnswer
print("correct answer")
sender.backgroundColor = .green
score += 1
else
print("wrong")
sender.backgroundColor = .red
print("(allQuestions.list[questionNumber].correctAnswer)")
//the following two lines change the right answer button to green using the tag value of the button
let correctAnswerButton = view.viewWithTag(selectedAnswer) as? UIButton
correctAnswerButton?.backgroundColor = UIColor.green
@IBAction func GoToNextQuestion(_ sender: UIButton)
questionNumber += 1
nextQuestion()
// selects a new questions and updates the score
func nextQuestion()
if questionNumber <= allQuestions.list.count - 1
questionLabel.text = allQuestions.list[questionNumber].question
questionImageView.image = UIImage(named: (allQuestions.list[questionNumber].questionImage))
optionAButton.setTitle(allQuestions.list[questionNumber].optionA, for: UIControlState.normal)
optionBButton.setTitle(allQuestions.list[questionNumber].optionB, for: UIControlState.normal)
optionCButton.setTitle(allQuestions.list[questionNumber].optionC, for: UIControlState.normal)
optionDButton.setTitle(allQuestions.list[questionNumber].optionD, for: UIControlState.normal)
optionEButton.setTitle(allQuestions.list[questionNumber].optionE, for: UIControlState.normal)
selectedAnswer = allQuestions.list[questionNumber].correctAnswer
updateUI()
else
let alert = UIAlertController(title: "Great!", message: "Do you want to start over?", preferredStyle: .alert)
let restartAction = UIAlertAction(title: "Restart", style: .default) (UIAlertAction) in
self.restartQuiz()
alert.addAction(restartAction)
present(alert, animated: true, completion: nil)
func updateUI()
scoreLabel.text = "score: (score)"
questionCounter.text = "(questionNumber + 1)/(allQuestions.list.count)"
func restartQuiz()
score = 0
questionNumber = 0
nextQuestion()
@IBAction func goBackToTopicsVC(_ sender: Any)
delegate?.goToTopicVC()
dismiss(animated: true, completion: nil)
indexPath.row
Topics are displayed as UIButton. I am able to go to the next view controller with Questions but I am not abe to fetch the questions swift file.
– user10304600
Sep 2 at 5:12
Cannot help you if you don't show your code
– sj-r
Sep 2 at 5:31
1 Answer
1
You can use the following steps :
1- Add a segue from TopicsViewController to QuestionsViewController and give the segue "Identifier Name " from Attributes inspector.
2- Add a variable in QuestionsViewController for the topic lets name it "topicType".
3- Override the below function in TopicsViewController and send the name of the topic with the segue:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if segue.identifier == "Identifier Name"
if let destinationviewController = segue.destination as? QuestionsViewController , let buttonPressed = sender as? UIButton
destinationviewController.topicType = buttonPressed.currentTitle!
4- For each button in TopicsViewController , get the button action and type the following function in it :
@IBAction func topicButton(_ sender: UIButton)
performSegue(withIdentifier: "Identifier Name", sender: nil)
I hope this helps you .
Hi Mina, I tried but I am not successful
– user10304600
Sep 3 at 5:09
Can you write the code and show the storyboard here so we can help you ??
– Mina Hanna
Sep 3 at 8:37
Where and how do I put the code? I am new to programming/swift and to stack overflow. I have tried to press every link on my initial post and on the "add comment" section to insert my code. I am unable to do it. I have read other post with four spaces and all other "tricks". But I am not able to. help!!!!
– user10304600
Sep 3 at 18:42
I added the code and the images to show the storyboard.
– user10304600
Sep 6 at 4:30
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
How are you displaying your topics? normally you would use tableview, so you would use
indexPath.row
to determine which the user tapped– sj-r
Sep 2 at 4:15