簡體   English   中英

按下按鈕會將文本發送到單獨的表視圖控制器中的單元格

[英]Pressing a button sends text to a cell in a separate table view controller

我當前正在創建一個包含以下內容的項目:

  • manifestViewController
  • 主視圖控制器(顯示子視圖控制器的4個按鈕)
  • 4個子視圖控制器
  • 表格視圖控制器(滑出面板)

我想在按下子視圖控制器上的特定按鈕(即按鈕#1)時將文本(即“按鈕”)發送到表視圖控制器第一部分的單元格。

我該怎么做呢? 這些按鈕不在表視圖控制器中,而是在單獨的視圖控制器中。

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.
    }
}

謝謝!

您可以通過不同的方式傳遞數據,其中之一是可以使用協議,並將數據從SubViewController傳遞到TableViewController

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

和您的SubViewController頁面創建協議的對象

var delegate : TableViewControllerProtocol?

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

並在TableViewController頁面中定義協議並創建協議方法並使用它

class TableViewController: UITableViewController ,TableViewControllerProtocol {

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

但不要忘記將TableViewController對象分配給SubViewController委托對象,否則明智的應用程序將崩潰

暫無
暫無

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

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