0

Complete newbie here, and I'm trying to make a button from a tabelViewCell display its row number when pressed.

Here's the code from tableViewCell:

    var myTableViewController: tableViewController?

    @IBOutlet var addButton: UIButton!


    @IBAction func addButtonPressed(_ sender: Any) { 
    myTableViewController?.addCellToHome(self) //call add function
}

And from tableViewController:

func addCellToHome(_ cell: UITableViewCell) //specifies what is to be done when addButton is pressed
{

    let row = tableView.indexPath(for: cell)

    print(row as! String)
}

Can someone please tell me what I'm doing wrong? I inserted a breakpoint inside addCellToHome and turns out it's never called. Completed stumped.

Thanks in advance.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
qunayu
  • 1,187
  • 1
  • 11
  • 18
  • Possible duplicate of [iOS Swift Button action in table view cell](http://stackoverflow.com/questions/28894765/ios-swift-button-action-in-table-view-cell) – DogCoffee Nov 26 '16 at 01:21
  • Are you sure the IBAction is being called? – TheValyreanGroup Nov 26 '16 at 01:28
  • @TheValyreanGroup Yes definitely. In my original code from tableViewCell it had `self.addButton.isEnabled = false` and when the button is pressed it greys out. – qunayu Nov 26 '16 at 01:31
  • The code you posted does not make sense. Each time the user taps the add button, you instantiate a new copy of your `myTableViewController`, call it's `addCellToHome()` function, and then discard the newly created view controller without displaying it. That is wrong. – Duncan C Nov 26 '16 at 02:01
  • What are you trying to do? Send a message from a button in a custom table view cell to the owning `UITableViewController`? – Duncan C Nov 26 '16 at 03:09
  • if you just need a single action from the whole cell just implement `tableView:didSelectRowAtIndexPath:` on the delegate, no button required. – markedwardmurray Nov 26 '16 at 07:51
  • Please don't include the answer in your question. If an answer solved your issue, mark the answer as accepted; you can also post your own answer if you want. Thank you. – Eric Aya Nov 26 '16 at 09:18

2 Answers2

0

I think you should either make a delegate protocol for the cell, something like MyCellDelegate.

See guide to delegates.

Or add an action to the cells button from cellForRow(at: IndexPath) in your tableViewController.

See guide to button actions

Community
  • 1
  • 1
Wiingaard
  • 4,150
  • 4
  • 35
  • 67
-1

My hunch is you're creating a new instance of tableViewController instead of using the current one that is in memory. Try the following instead of creating a new instance of the VC.

@IBAction func addButtonPressed(_ sender: Any) { 
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let myTableViewController = storyboard.instantiateViewController(withIdentifier :"tableViewController") as! tableViewController
    myTableViewController?.addCellToHome(self) //call add function
}

If you plan on using this VC in other method calls, you can also initialize the the myTableViewController outside of any function in the class like you did in your original post.

TheValyreanGroup
  • 3,554
  • 2
  • 12
  • 30
  • Got error: Value of type 'UITableViewController' has no member 'addCellToHome' – qunayu Nov 26 '16 at 01:50
  • Change `as! UITableViewController` to `as! tableViewController`, or whatever you class name is. And make sure you change the `withIdentifier` portion of the `instantiate` method to your VC's id in the storyboard. – TheValyreanGroup Nov 26 '16 at 01:51
  • @qunayu Glad i could help! – TheValyreanGroup Nov 26 '16 at 02:01
  • That code is creating a new throw-away view controller on each button press as well. That does not make sense. – Duncan C Nov 26 '16 at 02:02
  • @DuncanC From the sounds of it, he already has this VC on the navigation stack. Using this code does not create a new instance of the VC, it's using the one that is in memory. The OP's original code he was using _WAS_ creating a new instance of it, hence his function call was not working. – TheValyreanGroup Nov 26 '16 at 02:06
  • No, you are wrong. The function `instantiateViewController()` **always** creates a new instance of a view controller. The word "instantiate" means to create an instance. – Duncan C Nov 26 '16 at 02:07
  • @DuncanC Then why did this work and his original code not? I understand what you're saying, but I learned this is the correct way to use a currently stacked VC. Is there a better or more correct way? – TheValyreanGroup Nov 26 '16 at 02:23
  • @DuncanC Provide an answer then because I have been doing it this way for 3 years and it works. So what should be done? – TheValyreanGroup Nov 26 '16 at 02:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129082/discussion-between-duncan-c-and-thevalyreangroup). – Duncan C Nov 26 '16 at 03:04