my segmented control working fine.. long not attach target-action listener it. however, necessary me able detect events.
my code:
nsarray *itemarray = [nsarray arraywithobjects: @"following", @"everybody", @"nearby", nil]; uisegmentedcontrol *segmentedcontrol = [[uisegmentedcontrol alloc] initwithitems:itemarray]; uifont *font = [uifont fontwithname:@"patuaone-regular" size:12.0f]; uicolor *notchosenbuttoncolor = [uicolor colorwithred:(201.0/255.0f) green:(198.0/255.0f) blue:(191.0/255.0f) alpha:1.0]; uicolor *chosenbuttoncolor = [uicolor colorwithred:(235.0/255.0f) green:(218.0/255.0f) blue:(102.0/255.0f) alpha:1.0]; nsdictionary *normalattributes = [nsdictionary dictionarywithobjectsandkeys: font, uitextattributefont, notchosenbuttoncolor, uitextattributetextcolor, [uicolor clearcolor], uitextattributetextshadowcolor, nil]; nsdictionary *selectedattributes = [nsdictionary dictionarywithobjectsandkeys: font, uitextattributefont, chosenbuttoncolor, uitextattributetextcolor, nil]; [segmentedcontrol settitletextattributes:normalattributes forstate:uicontrolstatenormal]; [segmentedcontrol settitletextattributes:selectedattributes forstate:uicontrolstateselected]; [segmentedcontrol setbackgroundimage:[uiimage imagenamed:@"standard_bt.png"] forstate:uicontrolstatenormal barmetrics:uibarmetricsdefault]; [segmentedcontrol setbackgroundimage:[uiimage imagenamed:@"standard_bt_h.png"] forstate:uicontrolstateselected barmetrics:uibarmetricsdefault]; segmentedcontrol.frame = cgrectmake(5, 20, 280, 25); segmentedcontrol.segmentedcontrolstyle = uisegmentedcontrolstylebordered; [segmentedcontrol setdividerimage:[uiimage imagenamed:@"separator.png"] forleftsegmentstate:uicontrolstatenormal rightsegmentstate:uicontrolstatenormal barmetrics:uibarmetricsdefault]; [segmentedcontrol addtarget:self action:@selector(segmenttoggled:) forcontrolevents:uicontroleventvaluechanged]; [headerview addsubview:segmentedcontrol]; - (void)segmenttoggled:(uisegmentedcontrol*)sender { nsinteger index = sender.selectedsegmentindex; nslog(@"index: %d",index); if(index == 0){ sender.selectedsegmentindex = 0; [self.feeddescription removeallobjects]; dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{[[webapi sharedinstance]getfeed:0 max:100 count:5 controller:self];}); [self.collectionview reloaddata]; }else if(index == 1){ sender.selectedsegmentindex = 1; [self.feeddescription removeallobjects]; dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{[[webapi sharedinstance]getfeed:0 max:100 count:5 controller:self];}); [self.collectionview reloaddata]; }else if(index == 2){ sender.selectedsegmentindex = 2; [self.feeddescription removeallobjects]; dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{[[webapi sharedinstance]getfeed:0 max:100 count:5 controller:self];}); [self.collectionview reloaddata]; } }
how can implement target action listener above, , still update uisegmentedcontrols background images according to:
[segmentedcontrol setbackgroundimage:[uiimage imagenamed:@"standard_bt.png"] forstate:uicontrolstatenormal barmetrics:uibarmetricsdefault]; [segmentedcontrol setbackgroundimage:[uiimage imagenamed:@"standard_bt_h.png"] forstate:uicontrolstateselected barmetrics:uibarmetricsdefault];
try code:
- (void)segmenttoggled:(uisegmentedcontrol*)sender { (int i=0; i<[sender.subviews count]; i++) { if ([[sender.subviews objectatindex:i] respondstoselector:@selector(isselected)] && [[sender.subviews objectatindex:i]isselected]) { [sender setbackgroundimage:[uiimage imagenamed:@"standard_bt_h.png"] forstate:uicontrolstateselected barmetrics:uibarmetricsdefault]; } if ([[sender.subviews objectatindex:i] respondstoselector:@selector(isselected)] && ![[sender.subviews objectatindex:i] isselected]) { [sender setbackgroundimage:[uiimage imagenamed:@"standard_bt.png"] forstate:uicontrolstatenormal barmetrics:uibarmetricsdefault]; } } } hope
Comments
Post a Comment