unwind segue not triggering
Asked Answered
V

2

4

I have been learning swift and have made the foundation of most of my app. I have the following storyboard

app storyboard

Everything works fine. For example, I have an unwind segue on the add course view controller that triggers when you press save and you are returned to the 'your courses' view controller.

When you are on the my courses view controller, you can select a course and the topics are displayed, you can then select a topic and you are taken to an update score view controller, this all works fine.

However, my problem is this. I want to make it so that when you select save in the updatescore view controller, an unwind segue is triggered (the same as in the add course) and you are returned to the list of topics in the topics view controller.

However, I have followed many tutorials and obviously got it working before. (My action method for the unwind segue is in the correct topics view controller) but when i press save, the unwind segue is not returning me to the topics view controller.

Could anyone suggest a reason for this? I have spent a lot of time trying to find an answer and gone through many tutorials but have not managed to solve it.

I have also included a screen shot of the connections of the triggered segues for my save button to show that it is set up. Showing triggered segue for save button

i have the following code in the update score view controller

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if saveButton === sender {
        print("save button selected")
   }
}

But even this is not getting triggered when I click on save.

Many thanks

UPDATE: After following Ronatorys advice My view controller for the update score is as follows but it is still not working:

import UIKit

class UpdateScoreTableViewController: UITableViewController {

@IBOutlet weak var topicGettingUpdated: UITextField!
@IBOutlet weak var newScore: UITextField!


@IBOutlet weak var saveButton: UIBarButtonItem!


var index:Int?
var Topics:[String]!


var TopicToUpdate:String?


override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    guard let uiBarButtonItem = sender as? UIBarButtonItem else {
        print("There is no UIBarButtonItem sender")
        return
    }
}


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.section == 0 && indexPath.row == 0 {
        newScore.becomeFirstResponder()
    }
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

}

But the prepare for segue is not even getting triggered.

Vladimar answered 25/10, 2016 at 14:45 Comment(1)
Very difficult to help you with this problem without more information. The fact that the prepareForSegue isn't triggered merely confirms that the unwind segue isn't working.Fosterfosterage
S
1

Like Craig in the comments said, it's not that easy to find the problem. So I just build a simple app where you can follow the steps as guide and see if you forgot something to setup the functionality right. Hope it will help you. Note: Code is in Swift 3.0, but should be easy to adopt to 2.*

1. Storyboard with two View Controllers:

enter image description here

2. Declare the action method for the unwind segue in the FirstViewController.swift:

class FirstViewController: UIViewController {
  // action method for the unwind segue
  @IBAction func updateScore(_ segue: UIStoryboardSegue) {
    print("Back in the FirstViewController")
  } 
}

3. Connect the Save button in the Storyboard with the action method (with ctrl + drag):

enter image description here

4. Connect your Save button with the SecondViewController.swift file, to use it for checking in your prepareSegue method (with ctrl + drag):

enter image description here

5. Add the prepare(for:sender:) method to your SecondViewController.swift:

class SecondViewController: UIViewController {

  @IBOutlet weak var saveButtonPressed: UIBarButtonItem!

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // check safely with guard that your save button is the sender and you can use it
    // if not print message
    guard let uiBarButtonItem = sender as? UIBarButtonItem else {
      print("There is no UIBarButtonItem sender")
      return
    }

    // check if you selected the save button
    if saveButtonPressed == uiBarButtonItem {
      print("save button selected")
    }
  }
}

Result:

enter image description here

The sample app you can find here

Subdeacon answered 25/10, 2016 at 19:32 Comment(1)
Hi Ronatory, Thanks so much for this, but it has not worked. I tried following all your steps, but still nothing happens when I press save. The prepare for segue is not even getting triggered and "There is no UIBarButtonItem sender" is not being shown in the console. I thought maybe there is something I do not understand about navigation controllers. So I have removed the navigation controller that the update score was embedded in and everything works fine and I understand this i.e. I can navigate back through the stack. It is at this point that I can not get that save button to work.Vladimar
V
0

I did not manage to get the unwind segue to work but instead used

navigationController!.popViewControllerAnimated(true)

as a work around and this works fine.

Vladimar answered 13/11, 2016 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.