gpt4 book ai didi

ios - 按下按钮将文本发送到单独的 TableView Controller 中的单元格

转载 作者:行者123 更新时间:2023-11-28 07:58:03 24 4
gpt4 key购买 nike

我目前正在创建一个包含以下内容的项目:

  • revealViewController
  • 主视图 Controller (4 个显示 subview Controller 的按钮)
  • 4 个 subview Controller
  • 表格 View Controller (滑出式面板)

我想在按下 subview Controller 上的特定按钮(即按钮#1)时将文本(即“按钮”)发送到 TableView Controller 第一部分中的单元格。

我该怎么做呢?这些按钮不在 TableView Controller 中,而是一个单独的 View Controller 。

SubViewController.swift

import UIKit
class SubViewController: UIViewController {

@IBOutlet weak var button1: UIButton!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

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

@IBAction func button1action(_ sender: Any) {


//Pressing this button should send this text "button #1 pressed" to the cell in the TableViewController

//i.e. A new cell with the text "button #1 pressed" should appear on my table in the first section

}

}

TableViewController.swift

import UIKit

class TableViewController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()


// 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 4
}

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


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

cell.textLabel?.text = "add new item"

return cell
}
}

MainViewController.swift

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var SlideOutPanel: UIBarButtonItem!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

SlideOutPanel.target = self.revealViewController()
SlideOutPanel.action = #selector(SWRevealViewController.revealToggle(_:))
}

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

谢谢!

最佳答案

您可以以不同的方式传递数据,其中一种是您可以使用协议(protocol)并将数据从 SubViewController 传递到 TableViewController

protocol TableViewControllerProtocol: class {
func passData(With message:String)
}

和您的 SubViewController 页面创建您的协议(protocol)对象

var delegate : TableViewControllerProtocol?

@IBAction func button1action(_ sender: Any) {
delegate.passData(message: "button #1 pressed")
}

并在 TableViewController 页面中定义协议(protocol)并创建协议(protocol)方法并使用它

class TableViewController: UITableViewController ,TableViewControllerProtocol {

func passData(With message:String){
print(message)
}
}

但不要忘记将 TableViewController 对象分配给 SubViewController 委托(delegate)对象,否则应用程序将崩溃

关于ios - 按下按钮将文本发送到单独的 TableView Controller 中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47426715/

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