0

I have a custom cell with a button inside. Is there a way I can access a variable from mainVC.m to customCell.m in the buttons action method?

I can do like this: mainVC main = [[mainVC alloc] init];, but that would make a new main file. I need to access the original.

Update

What I'm trying to do is get the index path of which cell the button that was clicked is on. Then add that index path to a mutableArray which is declared in mainVC.m.

Or, the opposite, if it's possible, I would like to access a variable in customCell.m from mainVC.m in the class of heghtForRowAtIndexPath

  • 1
    http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Hot Licks Feb 10 '15 at 02:33
  • @HotLicks This is not between 2 view controllers. notice in the first answer: or "Passing Data Forward using Segue's" or "Passing Data Back" Does it speak about my questions, when there is no forward or backward segue? –  Feb 10 '15 at 02:46
  • 1
    OK, ignore the several techniques discussed for communicating between classes. – Hot Licks Feb 10 '15 at 02:47
  • @HotLicks didn't find my answer. I don't know what your talking about. –  Feb 10 '15 at 02:50

5 Answers5

1

You could set the tag property in the button in the cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 ...

 cell.button.tag = indexPath.row;
 ...
}

then on the action

-(IBAction)buttonPressed:(id)sender{
    UIButton *button = (UIButton *)sender;
    NSLog(@"%d", [button tag]);
}

Of course this index should refer to the index of some array on your VC

dminones
  • 2,236
  • 20
  • 21
1

Give tag for UIButton using inspector in Custom Cell(say, tag is 1). Then create button based on that tag in cellForRowAtIndexPath, then assign indexpath.row value as tag for created button.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIButton *titleButton = (UIButton *)[cell viewWithTag:1];
    titleButton.tag = indexPath.row;
    [titleButton addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
}

button action, you can use

-(IBAction)btnSelected:(id)sender
{
   UIButton *btn = (UIButton *)sender;
   NSLog(@"%ld",(long)[btn tag]);
}

In button action, you will get tags. Those tags you can use for your array to get appropriate values.

Suresh Vutukuru
  • 211
  • 2
  • 8
0

An effective approach would be to define a delegate protocol in mainVC and then assign the customCell object as the delegate. Depending on the relationship between the objects as they exist now you may already be able to get a reference to the existing mainVC. Honestly, we'd need to say way more code than that single proposed line.

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
0

In your cell add a property which is a pointer to the variable type you are wanting to use. When you create the cell, set this property to point to the variable. I am assuming mainVC.m is the controller with the table in it?

Or, add a property to the cell which is a block of the form @property (nonatomic,copy) void (^onButtonAction)(UIButton *button) which you call from inside the cell when the button is used. Again you can assign this when you create the cell. The block is defined in the table view controller so can access the variables of the view controller.

Better, why not avoid having buttons and just use cell selection. You can detect which row is selected by implementing:

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

Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
  • How would I get the cell of the selected button? –  Feb 10 '15 at 02:01
  • cuz there will be 2 separate actions. 1 - in `didSelectRowAtIndexPath` and 2 - If I can get the indexPath cell of the selected button –  Feb 10 '15 at 02:03
  • I would not use buttons and use cell selection. If you need the UITableViewCell add it to the block parameters: `@property (nonatomic,copy) void (^onButtonAction)(UITableViewCell *inCell, UIButton *button)`. In the block you now have the cell and the button. – Rory McKinnel Feb 10 '15 at 02:03
0

There are many ways to get the indexPath of cell from the button was clicked: 1. Protocol

// In your customCell.h file have a Delegate method

@protocol customCellDelegate <NSObject>

- (void)btn_ClickedForCell:(id)cell;

@end
@interface customCell : UITableViewCell
@property (nonatomic,assign)id<ChatOptionsPopUpDelegate>delegate;

// In your customCell.m File call this delegate method on click of button

- (void)btnAction{
  if ([_delegate respondsToSelector:@selector(btn_ClickedForCell:)])       {
        [_delegate btn_ClickedForCell:self];
    }
}

// Now in you mainVC.m File assign this delegate

@interface mainVC () <customCellDelegate>

// in your `cellForRowAtIndexPath`
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 ...

 cell.delegate = self;
 ...
}

// So whenever your button gets clicked the function will be called

- (void)btn_ClickedForCell:(id)cell{
      NSIndexPath *indexPath = [[self tableView] indexPathForCell:cell];
     // Enjoy you got the indexPath
}
  1. By Tag

// give tag to you button and get its cell by tag

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 ...

 cell.button.tag = indexPath.row;
 [cell.button addTarget:self action:@selector(btn_Clicked:) forControlEvents:UIControlEventTouchUpInside];
 ...
}

- (void)btn_Clicked:(UIButton*)sender{
         int tag = sender.tag;
          NSIndexPath *indexPath = [NSIndexPath indexPathForRow:tag inSection:0];
       // Enjoy you got the tag
}
haresh
  • 486
  • 3
  • 18