Reputation: 3455
I am using Segment Control. it contains two data. and one data is default and i need to display default in segment.
I am setting default data like this:
- (void)awakeFromNib
{
[super awakeFromNib];
[priceOptionSeg addTarget:self action:@selector(segmentedControlValueDidChange:) forControlEvents: UIControlEventValueChanged];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
[self assignDO:priceList];
}
- (void)assignDO:(NSMutableArray *)inputList
{
for (int x = 0; x < inputList.count; x++)
{
if (tempDO.price_is_default == YES)
{
[priceOptionSeg setSelectedSegmentIndex:x];
NSLog(@"%ld",(long) priceOptionSeg.selectedSegmentIndex);
}
}
}
I am mananing index like this way:
- (void)segmentedControlValueDidChange:(UISegmentedControl *)segment
{
switch (segment.selectedSegmentIndex)
{
case 0:
NSLog(@"First was selected");
break;
case 1:
NSLog(@"Second was selected");
break;
default:
break;
}
}
I am able to add default value in segment control. I can change values in segment. suppose I select 2nd segment but when I move to next controller and back to my controller segment is at first index.i want like if i move to next controller but when I come at my controller. last selected segment should be there what to do
Upvotes: 1
Views: 230
Reputation: 1167
In your ViewController.h
declared int like
@property(nonatomic,assign)int selectedIndex;
in ViewDidLoad
assign the default value
self.selectedIndex = 0;
in CellForRowAtIndexpath
selected index
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Create you custom class cell here
..
[cell.priceOptionSeg addTarget:self action:@selector(segmentedControlValueDidChange:) forControlEvents: UIControlEventValueChanged];
[cell.priceOptionSeg setSelectedSegmentIndex:self.selectedIndex];
..
}
and add the value change action of segment in same view controller
- (void)segmentedControlValueDidChange:(UISegmentedControl *)segment
{
switch (segment.selectedSegmentIndex)
{
case 0:
self.selectedIndex = 0;
break;
case 1:
self.selectedIndex = 1;
break;
default:
break;
}
}
From your existing Cell SubClass remove Following line
[priceOptionSeg addTarget:self action:@selector(segmentedControlValueDidChange:) forControlEvents: UIControlEventValueChanged];
Upvotes: 1