0

I've got the following situation: I've got a UISwipeGestureRecognizer hooked up on my table cell. On swipe, two buttons get their .hidden-status switched to NO.

Those cells are custom made. Meaning, the IBAction of those buttons is in that controller. Now I'd like the UIButton in those custom cells to send back a variable to the parent view. That parent view is the UIViewController (containing a UITableView).

How can I do this? I've read about protocols, but I can't get a solid example for that... Or are there any other ideas? They are all welcome :)

1
  • If you have control over your view hierarchy, the superview property might be helpful. May 2, 2011 at 12:16

1 Answer 1

1

The approach you are following may result in a crash or a memory leak as you have to retain the controller of your uitableview cell content.

However protocols are simple approach where after you create your table view cell you can allocate the viewcontroller (which creates the table view) as a delegate to all actions for the cell's content view.

In your custom cell's controller just call the delegate method and it will be handled by your parent view. It may sound confusing, but its really simple. Will try to post sample code.

@protocol yourDelegate;

@interface yourViewController{
id<yourDelegate> delegate;
}
@property (nonatomic,assign) id<yourDelegate> delegate;
@end
@protocol yourDelegate
- (void)passThisActionToMainController;
- (void)passThisEventToMainController;
@end

Once you are done with the above in your table view cell view controller, in your main controller create an instance of your view controller and then

controller.delegate = self;

In your cell's view controller when a user taps or performs any action then call

[delegate passThisActionToMainController];

or

[delegate passThisEventToMainController];

This way your main controller gets the event and it can be processed.

Alternate way to do this would be to create the buttons when you create the cell and handle the events. I would recommend this approach.

[myButton addTarget:self action:@selector(whatMyButtonShouldDo:)   
  forControlEvents:UIControlEventTouchUpInside];

The above line is copied from an answer here.

10
  • Thanks, and you were right. It didn't sound logical, at all, to me :) Some example code would really help, thanks for your time :D
    – Joetjah
    May 2, 2011 at 11:03
  • @Praveen S: Have you got a nice example for me on using protocols? I'd like to use that, but all the examples on the internet/fora are vague to me...
    – Joetjah
    May 2, 2011 at 13:03
  • @Praveen S: Hey, thanks for the update. I've gotten as far as this before, but I have absolutely no clue where the chunk of @protocol yourDelegate; ... @end should be, nor do I know what controller is in controller.delegate = self. I fail to see the logics in here :(
    – Joetjah
    May 2, 2011 at 13:25
  • Ok, @protocol should be in your view controller from where you will pass the handling to another view controller(main controller). And controller.delegate will be in function where you create your child view controller.
    – Praveen S
    May 2, 2011 at 13:27
  • That way it will pass the handling to parent from the child view controller.
    – Praveen S
    May 2, 2011 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.