Ios--uibutton Response Pass Parameters

Source: Internet
Author: User

- (void) AddTarget: (ID) Target action: (SEL) Action forcontrolevents: (uicontrolevents) controlevents; The method is unable to pass the parameter, can get only the UIButton of the response. Let's learn how to "pass parameters" through UIButton. We use UITableView as an example to define a cell in UITableViewCell, which we call a UIButton control on Customcell,cell. What we're going to do is get the cell when we click UIButton.      This can be done in two ways. 1. Using delegate first, define protocol in the Customcell class file, as defined below@protocolCustomcellprotocol- (void) Customcell: (Customcell *) cell Didtapbutton: (UIButton *) button;@endAdd the UIButton response to the Customcell file, for example, this response is called-(Ibaction) Buttontarget: (ID) sender; This method is called when the button is clicked. This method can be implemented as follows,-(Ibaction) Buttontarget: (ID) sender{if([Self.Delegaterespondstoselector: @selector (Customcell:didtapbutton:)]) {[Self.Delegateperformselector: @selector (Customcell:didtapbutton:) withobject:self WithObject:self.button]; }} Then implement the Proxy method in Uiviewcontroller, so that you can get the UITableViewCell of the UIButton. 2When you add a response to the second method in Viewcontroller-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath; Add a response directly to the UIButton method. Implemented as follows,-(UITableViewCell *) TableView: (UITableView *) Atableview Cellforrowatindexpath: (Nsindexpath *) indexpath{Customcell*cell = [Atableview dequeuereusablecellwithidentifier:@"Customcell"]; [Cell.button addtarget:self Action: @selector (Didtapbutton:) forcontrolevents:uicontroleventtouchupinside];returnCell;} Then when you click on the button, it will be called- (void) Didtapbutton: (UIButton *The rest of sender is how to get the cell it's in by this sender. The way we do this,- (void) Didtapbutton: (UIButton *) sender{CGRect buttonrect=Sender.frame;  for(Customcell *cellinch[TableView Visiblecells]) {    if(Cgrectintersectsrect (Buttonrect, Cell.frame)) {//cell is what you get.    }  }}

Ios--uibutton Response Pass Parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.