放松segue不解开

时间:2017-06-02 01:02:13

标签: ios swift swift3 segue

我正在尝试创建一个简单的展开segue,但显然我在某个地方犯了一个错误。我想要展开的保存按钮会对按下做出反应,但不会放松到表格视图。提前谢谢,我很感激。

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

和我的视图控制器

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

1 个答案:

答案 0 :(得分:0)

这是我创建展开细分的方式:

  1. 在第一个View Controller中编写unwind segue的代码:

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

  3. 给segue一个标识符: Adding an identifier.

  4. 然后在代码中调用它:

    @IBAction func UnwindButton(_ sender: UIButton) {
        performSegue(withIdentifier: "UnwindSegue", sender: UIButton())
    }
    
  5. 希望这有帮助!!!