Swift 如何在没有segue的情况下将数据从UITableView传递到UIViewController
我需要将Swift 如何在没有segue的情况下将数据从UITableView传递到UIViewController,swift,uitableview,uiviewcontroller,Swift,Uitableview,Uiviewcontroller,我需要将UITableView中的行标题传递给我的ViewController。我在故事板中没有VC(我是通过代码创建的)。所以我不能用segue func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) as! UITableViewCell cell.textLabel?.tex
UITableView
中的行标题传递给我的ViewController
。我在故事板中没有VC(我是通过代码创建的)。所以我不能用segue
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! UITableViewCell
cell.textLabel?.textColor = UIColor(red: 0, green: 122/255, blue: 1, alpha: 1)
cell.textLabel?.tintColor = UIColor(red: 0, green: 122/255, blue: 1, alpha: 1)
var titleOfFood = String()
if searching == true {
titleOfFood = searchedFoods[indexPath.row].title
cell.textLabel?.textColor = UIColor(red: 0, green: 122/255, blue: 1, alpha: 1)
print(titleOfFood)
} else {
titleOfFood = foods[indexPath.row].title
cell.textLabel?.textColor = UIColor(red: 0, green: 122/255, blue: 1, alpha: 1)
print(titleOfFood)
}
let controller = EatenFoodViewController()
let transitionDelegate = SPStorkTransitioningDelegate()
controller.transitioningDelegate = transitionDelegate
controller.modalPresentationStyle = .custom
//controller.delegate = self
transitionDelegate.customHeight = 620
transitionDelegate.showIndicator = false
self.present(controller, animated: true, completion: nil)
}
我需要将
标题食品
传递给EatenFoodViewController
您不需要一个序列就可以将数据发送到目标vc,您只需这样做即可
controller.titleOfFood = arr[indexPath.row].title // assuming you have an array of models
或
controller.titleOfFood = cell.textLabel!.text!
如何从UITableView向TitleOfood行发送行标题,我无法理解var titleOfood=”“。这是什么。如何处理:)请参见编辑
var titleofood=“
是应该接收发送值的变量,您应该在目标vc中使用它
class EatenFoodViewController:UIViewController {
var titleOfFood = ""
}