Trouble passing variable through segue (Swift) - ios

I have a variable called correctAnswers = 0 at the top of my program, I have a function where every time it is called it adds 1 to the correctAnswers value. I tested that the function was actually changing the variable by making it print after every +1 and sure enough it goes 1, 2, 3, 4 and so on. However I have an end screen that shows the results after the program has reached its limit of questions (10 in this case)
EDIT #2: Here is my prepare for segue function
func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "EndScreenSegue") {
let secondViewController = segue.destination as! EndScreen
correctAnswers = sender as! Int
secondViewController.gso = self
}
}
And here is the code in my end screen:
var gso: GameScreen?
override func viewDidLoad() {
super.viewDidLoad()
gso = GameScreen()
print(gso?.correctAnswers ?? 100)
}
When I print correct Answers it is still 0.
EDIT #3 I also tried this method of passing it through a segue and it is always nil. Does the fact its not working have anything to do with the fact the variable is being changed in a function? thats the only reason I can possibly think of to explain why nothing is working.
func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "EndScreenSegue") {
let secondViewController = segue.destination as! EndScreen
secondViewController.recievedAnswers = correctAnswers
}
}
class EndScreen: UIViewController{
var recievedAnswers: Int!
override func viewDidLoad() {
super.viewDidLoad()
print(recievedAnswers!)
}
}
EDIT #4 Figured it out! I think.
It appears they changed prepareForSegue and it no longer works, yet xcode does not give you an error? Should another post be made about this??
Code that actually works:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "EndScreenSegue"{
let secondViewController = segue.destination as! EndScreen
secondViewController.recievedAnswers = correctAnswers
}
}

You're creating a new instance of GameScreen in the viewDidLoad method of your EndScreen view controller. You need to pass the GameScreen object from your previous view controller to this one.

In your segue you're setting the correctAnswers property of your EndScreen view controller, but you're printing the value of gso?.correctAnswers. A new gso was just instantiated prior to this print statement and (I'm assuming) has a default value of 0 which is why you're getting 0 printed out.
You either need to pass the GameScreen through the segue (assuming that you're segueing from GameScreen), like so:
func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "EndScreenSegue") {
let secondViewController = segue.destination as! EndScreen
correctAnswers = sender as! Int
secondViewController.gso = self
}
}
Or you can just pass the correctAnswers through the segue as you're doing but be sure to use that value, not gso?.correctAnswers
class EndScreen: UIViewController{
var correctAnswers: Int?
override func viewDidLoad() {
super.viewDidLoad()
print(correctAnswers ?? 100)
}
}

You are creating a new instance of GameScreen.
Delete this line gso = GameScreen() from viewDidLoad

For some reason it looks like prepareForSegue no longer works, yet xcode does not give an error when you use it. The correct format now is:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "EndScreenSegue"{
let secondViewController = segue.destination as! EndScreen
secondViewController.recievedAnswers = correctAnswers
}
}

Related

Changing label text of second ViewController upon clicking button in first ViewController

I'm new to Swift and to iOS Development. I currently have 2 ViewControllers, a button in the first and a label in the second one. I've connected the first button to the second ViewController and the transition works.
Now, when I try changing the label's text I get the error:
fatal error: unexpectedly found nil while unwrapping an Optional
value
.
Here you find my prepare function in the first ViewController:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mySegue" {
let vc = segue.destination as! SecondViewController
vc.secondResultLabel.text = "Testing"
}
}
Can it be that the label in the second ViewController is somehow protected ?
Thanks for the help
You need to pass the String to the SecondViewController instead of directing setting it, as the UILabel has not been created yet.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mySegue" {
let vc = segue.destination as! SecondViewController
vc.secondResultLabelText = "Testing"
}
}
And in your SecondViewController viewDidLoad method set the UILabel to be the string
var secondResultLabelText : String!
override func viewDidLoad() {
secondResultLabelText.text = secondResultLabelText
}
add a string variable in the second view controller
var labelText: String!
in second view controller also (in viewDidLoad)
self.secondResultLabel.text = self.labelText
then first view controller prepare for segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mySegue" {
let vc = segue.destination as! SecondViewController
vc.labelText = "Testing"
}
}
this is because second view controller's UILabel Outlet is not being initialized yet in prepare for segue
Rikh's answer is the same, both his answer and mine are the same
Welcome aboard :)
Your problem is that your SecondViewController, and more specifically vc.secondResultLabelText is not initiated when you call prepare, so secondResultLabel is actually nil at that time.
You need to add a variable to your SecondViewController like so:
var labelText: String = ""
And then set that value instead:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mySegue" {
let vc = segue.destination as! SecondViewController
vc.labelText = "Testing"
}
}
In viewWillAppear or viewDidLoad on your SecondViewController you can then use that value for your secondResultLabelText which is now ready, connected, and won't crash
secondResultLabelText.text = labelText
Hope that helps.
First take a global variable in SecondViewController... eg I took "secondViewControllerVariable". Then get the text you want to display in your SecondViewController.
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "mySegue"
{
let vc = segue.destination as! SecondViewController
vc.secondViewControllerVariable = "Your string you get in FirstViewController"
}
}
And then in your SecondViewController, in viewDidLoad method set the UILabel to be the string
var secondViewControllerVariable : String! // You have to declare this first in your SecondViewController Globally
override func viewDidLoad()
{
vc.secondResultLabelText.text = secondViewControllerVariable
}
That's it. Happy Coding.

Swift 3 - Prepare Segue

I have 3 scenes in my storyboard. My initial View Controller is a Navigation Controller, then there is a relationship root view controller to a UI ViewController (view controller a) and then I have a push segue from a button in the ViewController to the third ViewController (view controller b) in the scene. I have given the push segue an identifier. Now I am trying to prepare my segue in the 2nd view controller (view controller a) like so:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "HistorySegue" {
if let viewController = segue.destination as? HistoryController {
viewController.detailItem = barcodeInt as AnyObject
}
}
}
However when I run this code and push the button in controller a I get the following error:
fatal error: attempt to bridge an implicitly unwrapped optional containing nil
What am I doing wrong?
Replace your code with the following, it will not crash at least.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "HistorySegue" {
if let viewController = segue.destination as? HistoryController {
if(barcodeInt != nil){
viewController.detailItem = barcodeInt as AnyObject
}
}
}
}
It must be that barcodeInt is defined as an implicitly unwrapped optional, like:
var barcodeInt:Int!
In that case, if it is nil when assigning it to detailItem, because of the !, swift takes your word for it that there is a non-nil value in there and dereferences it. That's a runtime error. Your best bet is to avoid ! in code you write (it's ok to leave the Apple generated code for IBOutlets, for example) if at all possible and learn more about optionals before going back to implicitly unwrapped optionals. And then, still use them sparingly.
Safer code for your situation:
if let viewController = segue.destination as? HistoryController,
let barcodeInt = barcodeInt as? AnyObject {
viewController.detailItem = barcodeInt
} else {
NSLog("Error: expected barcodeInt to be set")
}
I had the same issue. The logic is that one first prepares the segue (loads the UIViewController referenced by the container view), assigns it to a variable, and then uses it in viewDidLoad(). This code should work:
Swift 4.2
// usually an IBoutlet
var viewController: HistoryController!
override func viewDidLoad() {
super.viewDidLoad()
viewController.detailItem = barcodeInt as AnyObject
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "HistorySegue"
{ viewController = segue.destination as! HistoryController }
}
detailItem could possibly be defined as an IBoutlet in HistoryController, it depends on the OP code. In my case, where I had two simple container views with a label each inside, this has been the final working code for the main view controller class:
import UIKit
class ViewController: UIViewController {
#IBOutlet var firstView: ReusableViewController!
#IBOutlet var secondView: ReusableViewController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
firstView.myLabel.text = "My 1st reuse!!!"
secondView.myLabel.text = "And that's the 2nd!"
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "segueFirstView")
{ firstView = (segue.destination as! ReusableViewController) }
if (segue.identifier == "segueSecondView")
{ secondView = (segue.destination as! ReusableViewController) }
}
}
With that I could finally change the text of the two different UILabel directly from the main controller!
For a detailed explanation of how to use the container views one may check this S.O. answer.

Pass data to another view controller in navigation controller

I have two view controllers (fitstViewController.swift and secondViewController.swift) in navigation controler.
My idea is that
In firstViewController there are a button and a textFIeld. When I click the button, value in the textField is passed to secondViewController.
However it makes an error "Thread 1: signal SIGABRT" in the first controller.
Here is the code in the first controller..
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destination = segue.destinationViewController as! UINavigationController
let detailController = destination.topViewController as! secondViewController
detailController.stockSymbol = textField.text
}
I added just one line in the second controller..
var stockSymbol:String!
How can it be solved?
Thank you in advance!
In second controller the declared variable should be
var stockSymbol:String?
You should not force unwrap it unless you are sure that it would never be nil.
Replace your old code by:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
guard let destination = segue.destinationViewController as? SecondViewController else { return }
destination.stockSymbol = textField.text
}

Swift passing arrays between 2 tableviews. Returns nil instead

I am pretty new to SWIFT coding. My intention is to pass 2 arrays with values from Viewcontroller1 to Viewcontroller2. But it returns me nil value in Viewcontroller2. Can someone advise me please?
Here is the partial code in ViewController1.
#IBAction func solve(sender: AnyObject) {
//pass information to the nextviewcontroller
func prepareForSegue ( segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "Solve") {
var svc = segue!.destinationViewController as ViewController2
svc.toPass = self.force
svc.toPass2 = stiffness
}
}
Here is the partial code in ViewController2.
class ViewController2: UITableViewController {
var toPass:[String]!
var toPass2:[String]!
override func viewDidLoad() {
super.viewDidLoad()
println(self.toPass)
println(self.toPass2)
// Do any additional setup after loading the view.
}
The full code can be found in here. https://github.com/cherrythia/SWIFTpassingarrays/blob/master/README.md
I have created project similar to yours (using your github link) and #Sebastian Wramba is correct. You have to separate these two functions:
#IBAction func solve_pressed(sender: AnyObject) {
self.performSegueWithIdentifier("Solve", sender: sender)
}
override func prepareForSegue ( segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "Solve") {
var svc = segue.destinationViewController as ViewController2
svc.toPass = self.force
svc.toPass2 = stiffness
}
}
Okay, solved (pun intended). You have to give your segue identifier, in this particular case, "Solve". In your first view controller, select push to View Controller2, go to the fourth tab and set identifier to "Solve".

Prepare for segue error in lldb

I'm calling it like this
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "hat") {
let itemVC: ItemViewController = sender.destinationViewController as ItemViewController
itemVC.myDelegate = self
itemVC.nameArray = self.nameArray
}
}
but the lldb freezes up once I call to go to the next view controller. Any idea whats going on?
I believe you want:
segue.destinationViewController
instead of:
sender.destinationViewController

Resources