I'm currently creating a project that consists of:
I'd like to send a text (ie "button") to a cell in the 1st section on the table view controller on pressing a specific button (iebutton #1) on a Sub view controller.
How do I go about doing this? The buttons are not in the table view controller, but a separate 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.
}
}
Thanks!
You can pass data in different different way one of is you can use Protocols and pass data from your SubViewController to TableViewController
protocol TableViewControllerProtocol: class {
func passData(With message:String)
}
and your SubViewController page create object of your protocol
var delegate : TableViewControllerProtocol?
@IBAction func button1action(_ sender: Any) {
delegate.passData(message: "button #1 pressed")
}
and in TableViewController page define protocol and create protocol method and use it
class TableViewController: UITableViewController ,TableViewControllerProtocol {
func passData(With message:String){
print(message)
}
}
but do not forgot to assign TableViewController object to SubViewController delegate object other wise app will crash
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.