0

My application has a structure like the following:

One UIView with 5 buttons (this is a navigation controller ). When each button is pressed I push a UItableView in the navigation controller. My problem now is that , I have created a couple of Custom Cells which have their own classes. Those cells have a few buttons in. When each of those is buttons is pressed I want o "PUSH" another view in the navigation controller. The problem is that when I create the selector for the button in the cell's class, It doesn't recognize the "self.navigationcontroller" to push the new view. How can I pass the navigation controller as a parameter in the init method of the cell for example?

Please don't propose me to pass the selectors as parameters. I want to have a class that initializes a custom UIToolBar later which will be put in almost all my view, so I will want to pass the navigation controller as parameter so I won't have to create all methods again in each of my views.

Panos
  • 7,227
  • 13
  • 60
  • 95

2 Answers2

1

You could do a few things:

  1. Use the delegate protocol design pattern. Create a delegate protocol for your cells and assign your view controller as the delegate of those cells. Then, when the buttons are pressed, send messages to the delegate (aka your view controller).
  2. Use NSNotifications and the NSNotificationCenter
  3. Use blocks (this isn't really a great route here though)
  4. Or you could just add your view controller as the target for the buttons in your cell like so:

    // this would be in the cellForRowAtIndexPath method of your UITableViewDataSource (probably your UIViewController)

    [cell.button1 addTarget:self action:@selector(cellButton1Pressed:) forControlEvents:UIControlEventTouchUpInside];

Michael Frederick
  • 16,664
  • 3
  • 43
  • 58
  • Can you give me an example (with some source if possible) on how to use the 1st proposition? – Panos Jun 04 '12 at 06:32
0

You can handle button action in ViewController instead of cell class.

Nuzhat Zari
  • 3,398
  • 2
  • 24
  • 36
  • Probably you didn't read all my question. Right? I don't want to create the button actions in my VC and pass the selectors are parameters, because when later I will want to have a UIToolBar with buttons that will be used in many VCs, I don't want to copy the selector methods in all those VCs. – Panos Jun 04 '12 at 06:29