gpt4 book ai didi

ios - 展开 segue 不展开

转载 作者:行者123 更新时间:2023-11-28 08:08:54 28 4
gpt4 key购买 nike

我正在尝试创建一个简单的 unwind segue,但显然我在某处犯了一个错误。我想展开的保存按钮对按下有反应,但不会展开到表格 View 。在此先感谢,我很感激。

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

和我的 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.
}
}

最佳答案

这就是我创建放松转场的方式:

  1. 在第一个 View Controller 中编写展开转场的代码:

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

希望这有帮助!!!

关于ios - 展开 segue 不展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44319300/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com