简体   繁体   中英

how to get value from sender option of uibutton

i created a custom table view cell with a button on it and targetof the button in the same file tableviewcell.m(base calsee UITableViewCell).

When i click on the button then action is get called .

what i have to do is to get the value from the sender option of that button.like shown in the figure want to get value of "badgesname".

在此处输入图片说明

What you should when you want to track some additional events from cells, not only default selection. For example you add some buttons.

  • Using UIView 's tag property is bad ! It's not easy to read code with tags. What will you do when you have not only rows but sections too?
  • Finding UITableViewCell as superview of your button is bad too ! Button can be added as direct UITableViewCell subview or as cell's contentView subview.
  • Don't try to determine cell by checking touch point, it's bad ! No comments..
  • Don't use associated objects here, it's really bad ! You can read article about runtime anti-pattens here: http://nshipster.com/associated-objects/

So what should we do? We should follow delegation pattern . It's good for scaling too. You can store links to any data you want in your cell.

Your cell's header:

@class CellWithButton;

@protocol CellWithButtonDelegate <NSObject>

- (void)cellWithButtonDidPressed:(CellWithButton *)cell;

@end

@interface CellWithButton : UITableViewCell

@property id anyData;
@property (nonatomic, weak) id <CellWithButtonDelegate> *delegate;

@end

Your cell's implementation:

- (void)buttonPressed:(id)sender
{
    [self.delegate cellWithButtonDidPressed:self];
}

Now your controller:

- (UITableViewCell *)tableView cellFor...
{
    CellWithButton *cell = ...;
    cell.anyData = myData;
    cell.delegate = self;
}

- (void)cellWithButtonDidPressed:(CellWithButton *)cell
{
    id myData = cell.anyData;
    // Wow! Now you have your data.
    // And you can even find `indexPath` for your cell
    // by calling [self.tableView indexPathForCell:cell].
}

you can get whole cell by doing this

UITableViewCell *cell = (UITableViewCell *)[button superView] superView];

then you can get your objects like this

UILabel *label = (UILabel *)[cell viewWithTag:1234];

First, in the button action, convert the point of the button to the tableview's indexPath, then u can use that indexPath to get the value from your data array or even the cell

    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    if (indexPath != nil)
    {
       //Do stuff with the data array
       //Object *obj = [dataArray objectAtIndex:indexPath.row]
       //Get the text from the array

       //OR

       //YourCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];    
       //NSString *labelString = cell.textLabel.text;
    }

Donot create target method in tablviewcell.m file.Just create an outlet of button and add target in base controller from where your are creating table and follow the process as decribed.
In You cellForRowAtIndexPath: method give button a tag with indexPath.row and in button's action method just get tag and call cellForRowAtIndexPath to get cell.By cell you can fetch all values

 -(void)BtnOfCellTapped:(id)sender
 {
     UIButton *btn = (UIButton*)sender;
     CustomCell *cell = (CustomCell*)[tblView cellForRowAtIndexPath:btn.tag];
    NSLog(@"%@",cell.badgeValue);
 }

You can use Associated Objects for this.

How to use Associated Objects?

To set a property

objc_setAssociatedObject(self, @"AnyString", object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

To get a property

objc_getAssociatedObject(self,@"AnyString");

For reference : http://nshipster.com/associated-objects/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM