gpt4 book ai didi

ios - Swift 4 准备(用于 segue :) not being called

转载 作者:可可西里 更新时间:2023-11-01 01:11:08 26 4
gpt4 key购买 nike

我有一个 Split View Controller ,当我单击 TableView 中的项目时,它不会调用我的 prepare(for segue:) 方法。这是我的 prepare(for segue:) 方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("Here");
if segue.identifier == "showPOsDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let object = objects[indexPath.row] as! NSDate
let controller = (segue.destination as! UINavigationController).topViewController as! POsDetail
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}

这是我的 Storyboard的截图。我不知道为什么没有调用此方法。

enter image description here

几天来我一直在尝试用 out 计算,我对它不起作用感到非常沮丧。

这是我的完整主 Controller :

import UIKit

class POsMaster: UITableViewController {

var POsDetailController: POsDetail? = nil
var objects = [Any]()


override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.
navigationItem.leftBarButtonItem = editButtonItem

let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
navigationItem.rightBarButtonItem = addButton
if let split = splitViewController {
let controllers = split.viewControllers
POsDetailController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? POsDetail
}

}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}

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

@objc
func insertNewObject(_ sender: Any) {
objects.insert(NSDate(), at: 0)
let indexPath = IndexPath(row: 0, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
}

// MARK: - Segues

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("Here");
if segue.identifier == "showPOsDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let object = objects[indexPath.row] as! NSDate
let controller = (segue.destination as! UINavigationController).topViewController as! POsDetail
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}

// MARK: - Table View

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}

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

let object = objects[indexPath.row] as! NSDate
cell.textLabel!.text = object.description
return cell
}

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
objects.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}


}

最佳答案

当您在 Storyboard中连接您的 segue 时,会打开一个弹出窗口,您必须检查您是否已从 Selection 组 而不是 Accessory 组 设置您的 segue, Selection group 连接将调用您的 prepareForSegue: 方法。

关于ios - Swift 4 准备(用于 segue :) not being called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46982090/

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