简体   繁体   中英

Unwind segue not unwinding

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.
}
}

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: 创建segue

  3. Give the segue an identifier: 添加标识符。

  4. then call it in code:

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

Hope this helps!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM