簡體   English   中英

segue之后viewDidLoad()沒有運行

[英]viewDidLoad() not running after segue

我正在執行從一個表視圖到另一個表視圖的搜索。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    NSLog("You selected cell number: \(indexPath.row)!")
    self.performSegue(withIdentifier: "types", sender: productList[indexPath.row])
}

它應該運行由ViewController的自定義類描述的新TableView的viewDidLoad() (在Storyboard中已聲明)

func viewDidLoad(parent: String) {
    print("This should print")
    super.viewDidLoad()
    //self.typeTableView.delegate = self
    //self.typeTableView.dataSource = self

    //Set reference
    ref = Database.database().reference()
    //Retrieve posts
    handle = ref?.child(parent).observe(.childAdded, with: { (snapshot) in
        let product = snapshot.key as? String
        if let actualProduct = product
        {
            self.productList.append(actualProduct)
            self.typeTableView.reloadData()
        }

    })


}

知道為什么會這樣嗎?

viewDidLoad沒有參數,需要override子句:

override func viewDidLoad() {
    ...
}

將導航控制器嵌入到目標控制器,並使用標識符類型從當前表視圖單元格對其進行選擇。 然后在func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath){}之后添加以下方法

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "types" {
        if let indexPath = tableView.indexPathForSelectedRow {
            let object = productList[indexPath.row] as! yourProductType
            let controller = (segue.destination as! UINavigationController).topViewController as! YourDestinationViewController
            controller.yourProductProperty = object
        }
    }
}

確保在Destination控制器中聲明yourProductProperty,以便可以訪問其中的當前產品對象。

您的方法簽名是

func viewDidLoad(parent: String) {

但這應該是

override func viewDidLoad() {
        super.viewDidLoad()
        // Your code
    }

您在tableviewcell使用同一類嗎? 如果是,則為兩個tableview保留不同的標識符[RESUE IDENTIFIER]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM