iOS中通过segue传值的问题

 ll等待花开的大婶_oO_107 发布于 2022-10-28 06:22

自己在跟一个教程入门iOS,有些问题不太清楚,希望得到解答。
简单的说就是checklist对象通过sender从AllListsTableViewController传送到ChecklistViewController中,其中有用到的AllListsTableViewControllerprepare()tableView(didSelectRowAt)两个方法,不太清楚执行顺序。

AllListsTableViewController.swift

import UIKit

class AllListsTableViewController: UITableViewController {

    var lists: [Checklist]
    
    required init?(coder aDecoder: NSCoder) {
        lists = [Checklist]()
        super.init(coder: aDecoder)
        
        var list = Checklist(name: "Birthdays")
        lists.append(list)
        
        list = Checklist(name: "Groceries")
        lists.append(list)
        
        list = Checklist(name: "Cool App")
        lists.append(list)
        
        list = Checklist(name: "To Do")
        lists.append(list)
    }
    
    //点击任意一行,都需要吧对应的checklist对象放进prepare中
    //prepare在viewDidLoad()之前
    //通过sender 送到ChecklistViewController之后,viewDidLoad()才能更改标题
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "ShowChecklist" {
            let controller = segue.destination as! ChecklistViewController
            controller.checklist = sender as! Checklist
        }
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return lists.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
        let cell = makeCell(for: tableView)
        let checklist = lists[indexPath.row]
        cell.textLabel!.text = checklist.name
        cell.accessoryType = .detailDisclosureButton

        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let checklist = lists[indexPath.row]
        //点击后把checklist放进segue
        performSegue(withIdentifier: "ShowChecklist", sender: checklist)
    }
    
 
    func makeCell(for tableView: UITableView) -> UITableViewCell {
        let cellIdentifier = "Cell"
        if let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) {
            return cell
        } else {
            //如果table view找不到一个cell去重用,就会到else这去手动创建一个cell
            return UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
        }   
    }
}

想了想应该就是对prepare()不太了解,不太清楚它什么时候调用,点击row后触发tableView(didSelectRowAt)后把checklist对象放入segue中,但prepare()是什么时候触发呢?

2 个回答
  • 1.didSelect
    2.performSegue(withIdentifier: "ShowChecklist", sender: checklist)
    3.prepare(for segue: UIStoryboardSegue, sender: Any?)

    其实你在方法里面写个自定义的打印,就知道执行顺序了

    2022-11-12 01:42 回答
  • 想要知道执行顺序一般有两种方式:

    1. 设置打印点,就是print函数了

    2. 设置断点,像你这样的问题就可以设置符号断点,然后查看调用栈,调用顺序就一目了然了

    2022-11-12 01:42 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有