1

I'm trying to create a simple unwind segue but apparently I've made a mistake somewhere. The save button I would like to unwind reacts to pressing but doesn't unwind to the table view. Thanks in advance I appreciate it.

import UIKit

class NoteTableViewController: UITableViewController {

var notes = [Note]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

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

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 0
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 0
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
    return cell
}

//ACTION
@IBAction func unwindToNoteList(sender: UIStoryboardSegue) {
    if let sourceViewController = sender.source as? ViewController, let noteTaken = sourceViewController.noteTaken {

        // Add a new meal.
        let newIndexPath = IndexPath(row: notes.count, section: 0)

        notes.append(noteTaken)
        tableView.insertRows(at: [newIndexPath], with: .automatic)
    }
}
}

and my view controller

import UIKit
import os.log

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var typeField: UITextField!
@IBOutlet weak var textDisplay: UILabel!
@IBOutlet weak var saveButton: UIBarButtonItem!

var noteTaken: Note?

func textFieldDidEndEditing(_ textField: UITextField) {
    textDisplay.text = typeField.text
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    // Hide the keyboard.
    typeField.resignFirstResponder()
    return true
}

override func viewDidLoad() {
    super.viewDidLoad()
    typeField.delegate = self
    // Do any additional setup after loading the view, typically from a nib.
}

//NAV
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    super.prepare(for: segue, sender: sender)

    // Configure the destination view controller only when the save button is pressed.
    guard let button = sender as? UIBarButtonItem, button === saveButton else {
        os_log("The save button was not pressed, cancelling", log: OSLog.default, type: .debug)
        return
    }

    let note = typeField.text ?? ""

    // Set the meal to be passed to MealTableViewController after the unwind segue.
    noteTaken = Note(note: note)
}

@IBAction func cancelButton(_ sender: UIBarButtonItem) {
    dismiss(animated: true, completion: nil)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Tom
  • 81
  • 10
  • Control click on your save button and make sure its triggered segue action is connected to `unwindToNoteListWithSender:`. – vacawama Jun 02 '17 at 01:23
  • @Tom please check this, is this useful. https://stackoverflow.com/questions/41074677/how-to-unwind-to-the-first-view-controller-on-a-navigation-stack/41075029#41075029 – Nandhakumar Kittusamy Jun 02 '17 at 01:23
  • Also, don't call `super.prepare(for: segue, sender: sender)`. That isn't necessary (and might be detrimental). – vacawama Jun 02 '17 at 01:27
  • Thanks, I've looked into these but I am still having the issue. – Tom Jun 02 '17 at 05:15

1 Answers1

0

This is how I create my unwind segues:

  1. Write the code for the unwind segue in the first View Controller:

    @IBAction func unwindSegue(Segue: UIStoryboardSegue) {
    // Run code when the view loads up
    }
    
  2. Create the segue in main.storyboard: Creating the segue

  3. Give the segue an identifier: Adding an identifier.

  4. then call it in code:

    @IBAction func UnwindButton(_ sender: UIButton) {
        performSegue(withIdentifier: "UnwindSegue", sender: UIButton())
    }
    

Hope this helps!!!

Mikael Weiss
  • 839
  • 2
  • 14
  • 25