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
Related
I have a MeterTableviewController connected to two tableview controllers with two segues. The first segue LocationListSegue is connected from a button on the MeterTableviewController directly to the LocationListTableViewController. Another segue MetersListSegue connected from the top of the MeterTableViewController to the
MeterListTableviewController.
When I press on the location button, the LocationListTableViewController gets loaded twice.
I thought it is because I was calling it twice: once from the button IBAction and another time on Prepare, so I commented the code from Prepare, but it still loads it twice.
However the MeterListTableViewController gets loaded only once.
I dont know why the button press loads the controller twice. Can someone tell me?
#IBAction func chooseLocation(_ sender: Any) {
self.performSegue(withIdentifier: "LocationListSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
/*
if segue.identifier == "LocationListSegue"{
let viewController:LocationListTableViewController = segue.destination as! LocationListTableViewController
}
*/
if segue.identifier == "MetersListSegue"{
let viewController2:MeterListTableViewController = segue.destination as! MeterListTableViewController
}
}
You have added a segue as well as segue action from button, remove one of them
Remove story board segue and change your code with my code. This is enough for you.
#IBAction func chooseLocation(_ sender: Any) {
let tableViewController = self.storyboard?.instantiateViewController(withIdentifier: "your identifier") as! MeterListTableViewController
self.navigationController?.pushViewController(tableViewController, animated: true)
}
If you want to use segue remove IBAction for UIButton and write this code.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "LocationListSegue"{
let viewController:LocationListTableViewController = segue.destination as! LocationListTableViewController
} else if segue.identifier == "MetersListSegue"{
let viewController2:MeterListTableViewController = segue.destination as! MeterListTableViewController
}
}
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
}
}
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
}
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".
I don't get what I am doing wrong, I am trying to perform a push segue by an identifier so that I can move to other view-controller when an IF condition gets TRUE
the segue is always performed either I add this method : override func prepareForSegue()
#IBAction func btnClicked(sender: AnyObject) {
if(n.isEqualToString("mpc01") && p.isEqualToString("mpc01"))
{
self.performSegueWithIdentifier("login_to_dashboard", sender: nil)
}
else{
println("no data retrieved")
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "login_to_dashboard" {
// let secondViewController = segue.destinationViewController as VC_dashboard
println("performed segue")
}
else{
println("recieved other command")
}
}
my ViewController storyboard ID : SID_login
my SecondViewController storyboard ID : SID_dashboard
my Push Segue ID : login_to_dashboard
I want to perform segue by its ID only , not directly
You don't have the control when you use - (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender. Instead, you can have control to trigger the segue or not by using this: - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
The prepareForSegue just let you doing some custom works before triggering.
In prepareForSegue method you are checking if segue.identifier != "login_to_dashboard", I think you need to change it to == to perform segue.
#IBAction func btnClicked(sender: AnyObject) {
if(n.isEqualToString("mpc01") && p.isEqualToString("mpc01"))
{
self.performSegueWithIdentifier("login_to_dashboard", sender: nil)
}
else {
println("no data retrieved")
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "login_to_dashboard") {
let secondViewController = segue.destinationViewController as SecondViewController
println("performed segue")
}
else{
println("recieved other command")
}
}
You should use this method instead
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "login_to_dashboard" {
return true
}
else{
return false
}
}
this is very strange but since segue inherited from NSObject you can try:
if(segue.valueForKey("_identifier") as String == "login_to_dashboard"){
var targetController:VC_dashboard = segue.destinationViewController as VC_dashboard
targetController.doSomthing()
}
I did the mistake , solution to my question is to create a segue from view controller to other view controller rather a button to view-controller
I am sorry I am actually a beginner to xcode Swift , the question I asked was totally different to my segue issue. I actually created a segue directly from the button insteads of the first responser , thats why it always resulted to segue to another view-controller