ios - UISegmentedControl won't change value -
i trying create uisegmentedcontrol
programatically. have uiviewcontroller
in storyboard nothing in it.
.h file
uisegmentedcontrol *segmentedcontrol; nsstring *feedbackbuttontitle; nsstring *contactsbuttontitle;
and here property declarations.
@property (nonatomic,retain) iboutlet uisegmentedcontrol *segmentedcontrol; -(void) segmentedcontrolindexchanged;
in viewdidload:
have initalized , added uisegmentedcontrol
.
nsstring *language = [[nslocale preferredlanguages] objectatindex:0]; if ([language isequaltostring:@"en"]){ contactsbuttontitle = [[[configfiledictionary objectforkey:@"contacts"] objectforkey:@"label"] objectforkey:@"en"]; feedbackbuttontitle = [[[[[configfiledictionary objectforkey:@"contacts"] objectforkey:@"contact"]objectforkey:@"feedback"]objectforkey:@"label"]objectforkey:@"en"]; } else if([language isequaltostring:@"fr"]){ contactsbuttontitle = [[[configfiledictionary objectforkey:@"contacts"] objectforkey:@"label"] objectforkey:@"fr"]; feedbackbuttontitle = [[[[[configfiledictionary objectforkey:@"contacts"] objectforkey:@"contact"]objectforkey:@"feedback"]objectforkey:@"label"]objectforkey:@"fr"]; } nsarray *itemsarray = [[nsarray alloc] initwithobjects:contactsbuttontitle, feedbackbuttontitle, nil]; segmentedcontrol = [[uisegmentedcontrol alloc] initwithitems:itemsarray]; segmentedcontrol.segmentedcontrolstyle = uisegmentedcontrolstyleplain; //segmentedcontrol.selectedsegmentindex = 0; segmentedcontrol.frame = cgrectmake(0.0f, 0.0f, 320.0f,40.0f); [segmentedcontrol addtarget:self action:@selector(segmentedcontrolindexchanged) forcontrolevents:uicontroleventvaluechanged]; // [self.view addsubview:segmentedcontrol]; // create view contact display. [self createviews];
and (void)segmentedcontrolindexchanged
-(void)segmentedcontrolindexchanged { switch (self.segmentedcontrol.selectedsegmentindex) { case 0: [self createviews]; break; case 1: [self showfeedbackform]; break; default: break; } }
this showing segmented control on screen fine, when click on options in segmentedcontrol, goes option case 0. segmented control , opens [self createviews];
on inserting breakpoints @ line case 0
, noticed option _selectedsegment in segmentedcontrol 1. doesn't make sense.
ok created in uiviewcontroller
property segmentedcontrol
, wrote code:
- (void)viewdidload { [super viewdidload]; nsarray *itemsarray = [[nsarray alloc] initwithobjects:@"a", @"b", nil]; self.segmentedcontrol = [[uisegmentedcontrol alloc] initwithitems:itemsarray]; self.segmentedcontrol.segmentedcontrolstyle = uisegmentedcontrolstyleplain; self.segmentedcontrol.selectedsegmentindex = 0; self.segmentedcontrol.frame = cgrectmake(0.0f, 0.0f, 320.0f,40.0f); [self.segmentedcontrol addtarget:self action:@selector(segmentedcontrolindexchanged) forcontrolevents:uicontroleventvaluechanged]; [self.view addsubview:self.segmentedcontrol]; } -(void)segmentedcontrolindexchanged { switch (self.segmentedcontrol.selectedsegmentindex) { case 0: nslog(@"createviews"); break; case 1: nslog(@"showfeedbackform"); break; default: break; } }
it works me - logging proper choices.
as _segmentedcontrol
right - notice use dot notation segmentedcontrol. might case since when you're not using dot notation - you're not calling getter.
edit
to sum little - segmentedcontrol .h file redundant - it's uisegmentedcontrol
. property can referenced dot notation - self.segmentedcontrol
. or synthesized name (made default) _segmentedcontrol
.
so you're doing referencing .h object when initializing , adding uisegmentedcontroller
when segmentedcontrolindexchanged
called - change in property not visible.
Comments
Post a Comment