views:

85

answers:

3

When a button is pushed in one of my app's table view cells, I need to push a certain view controller onto the navigation stack.

This could be done by using an instance of NSNotification to inform the table view's controller of the button press. But that would be awfully heavyweight, especially since selections in a tab bar in the app could cause the table view to appear or disappear, creating additional overhead as the various table views register and unregister themselves whenever they are tabbed onto or off of the screen.

Can anyone think of a better solution?

+2  A: 

Why not put

[[self navigationController] pushViewController:targetViewController animated:YES];

in the method called by the button?

blindJesse
As far as I know, views are not aware of the navigation controller. But just to make sure, I tried that code and sure enough it crashed the app.
William Jockusch
Perhaps I am being naive here . . . the button is calls a method in myCell.m. If I could get it to call a method in myTableViewController.m, that would simplify things greatly, though there would still be some overhead involved in letting myTableViewController know which cell had its button pressed.
William Jockusch
Yeah I didn't know you were subclassing the cell. I guess if you must do that then a notification is the most logical thing, or a protocol. You could identify the cell by setting its tag value.Another option, if you must subclass the cell, would be adding the button to it after loading it from the nib, in the view controller's cellForRowAtIndexpath method, then hook it up in the VC.
blindJesse
I'm not sure you are doing this right. Obviously the tableViewController knows which cell is being pressed. You do not need an to use a UIButton's action method to accomplish this at all
willcodejavaforfood
A: 

Make your UITableViewController use the UITableViewDelegate Protocol and implement this method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

From the indexPath you can get which row has been pressed and then you know which cell is being selected. The purpose of the UITableViewController is to know about the cells and the cell itself does not need a button to trigger an event to push a new view.

willcodejavaforfood
A: 

What I did was set the table view's delegate to be the same as its controller. Then:

UITableView *myTableView = (UITableView *)self.superview;
NSIndexPath *indexPath = [myTableView indexPathForCell: self];
MyTableViewController *myTableViewController = (MyTableViewController *)(myTableView.delegate);
[myTableViewController buttonWasPressedOnCellWithIndexPath: indexPath];
William Jockusch
Did you try what I suggested?
willcodejavaforfood
Well, your suggestion helped me to think of what I ended up doing, so thanks! But you were answering a slightly different question from the one I asked. I was asking about pressing a button within a cell, which is different from selecting the cell.
William Jockusch