ios - Using protocols for detecting value changes in UISegmentedControl -


i have uisegmentedcontrol in uitableviewcell, need listen value changes in tableviewcontroller.

so far know have use protocols able this, i'm not sure put code. should in uitableviewcell? in viewcontroller? bit confusing me.

i need pinpointing on start, or better, code example?

here code using protocol.in sample , can observe value change of segmentcontrol. can know cell's segment being tapped , know segment data change.

// viewcontroller.m

#import "viewcontroller.h" #import "segmenttestcell.h"  @interface viewcontroller ()<uitableviewdatasource, uitableviewdelegate, segmenttestcelldelegate>  @property (nonatomic, strong) uitableview *table;  @end  @implementation viewcontroller #pragma mark - lifecycle - (void)viewdidload {     [super viewdidload];     // additional setup after loading view, typically nib.     [self.table registerclass:[segmenttestcell class] forcellreuseidentifier:@"cellid"];     [self.view addsubview:self.table]; }  - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }  #pragma mark - uitableviewdatasource, uitabbardelegate - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {     return 100; }  - (nsinteger)numberofsectionsintableview:(uitableview *)tableview {     return 1; }  - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath {     return 50; }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     segmenttestcell *cell = [tableview dequeuereusablecellwithidentifier:@"cellid"];     cell.indexpath = indexpath;     if (!cell.delegate) {         cell.delegate = self;     }     return cell; }  #pragma mark - segmenttestcelldelegate - (void)segmentchangeatindexpath:(nsindexpath *)indexpath selectindex:(nsinteger)selectedindex selecttitle:(nsstring *)selectedtitle {     nslog(@"%lu - %lu - %@",indexpath.section,indexpath.row,selectedtitle);      // }  #pragma mark - getter - (uitableview *)table {     if (!_table) {         _table = [[uitableview alloc]initwithframe:self.view.bounds style:uitableviewstyleplain];         _table.allowsselection = no;         _table.datasource = self;         _table.delegate = self;     }     return _table; }  @end 

// segmenttestcell.h

#import <uikit/uikit.h>  @protocol segmenttestcelldelegate <nsobject> @optional - (void)segmentchangeatindexpath: (nsindexpath *)indexpath selectindex: (nsinteger)selectedindex selecttitle: (nsstring *)selectedtitle; @end  @interface segmenttestcell : uitableviewcell  @property (nonatomic, strong) uisegmentedcontrol *segmentcontrol;  @property (nonatomic, strong) nsindexpath *indexpath; @property (nonatomic, weak) id<segmenttestcelldelegate> delegate;  @end 

// segmenttestcell.m

#import "segmenttestcell.h"  #define titles @[@"a",@"b",@"c"]  @interface segmenttestcell()  @end  @implementation segmenttestcell  - (void)awakefromnib {     [super awakefromnib];     // initialization code }  - (void)setselected:(bool)selected animated:(bool)animated {     [super setselected:selected animated:animated];      // configure view selected state }  - (instancetype)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier {     self = [super initwithstyle:style reuseidentifier:reuseidentifier];     if (self) {         self.segmentcontrol.frame = cgrectmake(20, 10, 100, 30);         [self.contentview addsubview:self.segmentcontrol];     }     return self; }  - (void)segmentvaluechange: (id)sender {     //     uisegmentedcontrol *control = (uisegmentedcontrol *)sender;     if ([self.delegate respondstoselector:@selector(segmentchangeatindexpath:selectindex:selecttitle:)]) {         [self.delegate segmentchangeatindexpath:_indexpath selectindex:control.selectedsegmentindex selecttitle:titles[control.selectedsegmentindex]];     } }  - (uisegmentedcontrol *)segmentcontrol {     if (!_segmentcontrol) {         _segmentcontrol = [[uisegmentedcontrol alloc]initwithitems:titles];         [_segmentcontrol addtarget:self action:@selector(segmentvaluechange:) forcontrolevents:uicontroleventvaluechanged];     }     return _segmentcontrol; }  @end 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -