0

I have a custom uitableviewcell and subclassed, and it is containing a uitextfield and delegate is also set, now when return key on keyboard is pressed I want to try few things

  1. perform a segue(but issue is I am in uitableviewcell subclass).
  2. modally present another view controller(but issue is uitableviewcell do not allow this).
  3. I want to display uiactionsheet(but again limitation is uitableviewcell).

If i get rootviewcontroller reference then rootviewcontroller's view itself not displayed or not the active view so any thing you do will not present on screen, active view is required.

S.J
  • 3,063
  • 3
  • 33
  • 66
  • You could always present your VC modally from the window's rootViewController, right? – Aaron Aug 12 '14 at 17:00
  • Is the problem that you're not able to get the `UIButton` action hooked up, or that you don't know how to present another view controller modally from the table view cell? – Aaron Aug 12 '14 at 17:04
  • -1 because question hasn't been clarified – Aaron Aug 12 '14 at 18:56
  • Thanks for clarifying your question. Unfortunately it's quite a bit different than what you originally asked. Also, based on your comment about the window's `rootViewController` not currently being displayed implies that perhaps you've presented some other view controller on top of it. – Aaron Aug 13 '14 at 16:16
  • Anyway, this isn't a limitation of the `UITableViewCell` it's just the way it works. @Bhaskar's approach with a delegate would work. You could also use blocks on your table view cell that would allow the table or it's view to do something when the cell's button is clicked. – Aaron Aug 13 '14 at 16:24
  • @Aaron please update your answer. – S.J Aug 13 '14 at 16:25
  • @Aaron please can you also provide a block approach code for tableview and cell. – S.J Aug 13 '14 at 16:30
  • Updated my answer, thoughts? – Aaron Aug 13 '14 at 22:39

3 Answers3

2

You could use a block property on your cell that is fired whenever your custom button action occurs. Your cell's block property might look something like this:

@interface CustomTableViewCell : UITableViewCell

@property (nonatomic, copy) void (^customActionBlock)();

@end

Your cell would then invoke this block from the custom button action like this:

@implementation CustomTableViewCell

- (IBAction)buttonTapped:(id)sender {
    if ( self.customActionBlock ) {
        self.customActionBlock();
    }
}

@end

Then finally, you set the block in -cellForRowAtIndexPath: back in your view controller (or wherever) like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];
    cell.textLabel.text = [self.colors objectAtIndex:indexPath.row];
    cell.customActionBlock = ^{
        NSLog(@"Do the stuff!");
        // present view controller modally
        // present an action sheet
        // etc....
    };
    return cell;
}

One word of caution, though. If you use blocks you run the risk of strongly referencing self and creating a memory leak. Blocks are fun and easy to use but you have to play by their rules. Here are some resources to help you get familiar with them:

Community
  • 1
  • 1
Aaron
  • 7,055
  • 2
  • 38
  • 53
  • please read the above comments, I already know these kind of ways but I am looking for a standard way. – S.J Aug 12 '14 at 17:19
  • There's no standard approach for what you're trying to do. There are pro's cons for all of it. This is _a_ standard way, unless you have an instance of the view controller in which your table view is contained. In which case you'd just call -presentViewController:animated:completion: on _that_ view controller. – Aaron Aug 12 '14 at 17:58
  • Nice. No one knows how to help you because your question is unclear. Are you asking "How do I present something modally?' or "How do I get a button tap to fire an even from a UITableViewCell?" or maybe it should be `Whats the best practice for presenting something modally from a button tap inside a table view cell?" Unclear questions are hard to answer. – Aaron Aug 12 '14 at 20:31
  • Ok, I have a custom uitableviewcell and subclassed, and it is containing a uitextfield and delegate is also set, now when return key on keyboard is pressed I want to try few things 1>perform a segue(but issue is I am in uitableviewcell subclass), 2>modally present another view controller(but issue is uitableviewcell do not allow this), 3> I want to display uiactionsheet(but again limitation is uitableviewcell), your answer will not work because rootviewcontroller's view itself not displayed or not the active view so any thing you do will not present on screen, active view is required. – S.J Aug 13 '14 at 09:38
  • Please view the question again I updated it. Thank you. – S.J Aug 13 '14 at 12:16
  • No sweat. Thanks for clarifying your question. – Aaron Aug 14 '14 at 15:54
1

You can attach action to your buttons even if they are in a tableView

[cell.button addTarget:self action:@selector(presentController:) forControlEvents:UIControlEventTouchUpInside];

presentController is referring to an IBAction

- (IBAction)presentController:(id)sender
{
  //present
}
meda
  • 45,103
  • 14
  • 92
  • 122
  • thanks for replying, Is creating a delegate is not a good approach? There are several ways but I am trying to pursue the standard way. – S.J Aug 12 '14 at 17:04
  • The standard way is to use the xcode drag and drop, but since cells are dynamic, you will need to do it programmatically as shown in my answer – meda Aug 12 '14 at 17:08
  • If I declare a delegate that notify the uitableviewcontroller, that display the uiactionsheet or modally present viewcontroller. – S.J Aug 12 '14 at 17:11
  • Yes then? What is the concern? – meda Aug 12 '14 at 17:27
  • Please view the question again I updated it. Thank you. – S.J Aug 13 '14 at 12:16
1

Implement button action in Tableview SuperClass. Or You can use Custom delegate in UITableViewCell subclass. In UITableView Subclass declare a protocol.

@protocol customCellDelegate <NSObject>

@required
-(void)selectedButtonInIndexPath : (NSIndexPath *)indexpath;
@end

Set this property in UITableView Subclass

@property (nonatomic, strong)NSIndexPath *indexpath;
@property (nonatomic, strong) id <customCellDelegate> delegate;

And then in Your UITableView Subclass Button action add This lines

 if(self.delegate){
    [self.delegate selectedButtonInIndexPath: self.indexpath];
 }

And in your tableview datasource method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

Implement this code

cell.delegate = (id)self;
cell.indexpath = indexPath;

And in Uitableview super class just implement this method

 -(void)selectedButtonInIndexPath : (NSIndexPath *)indexpath{
    //display uiimagepickercontroller modally or anything else here
 }
BHASKAR
  • 1,201
  • 1
  • 9
  • 20