gpt4 book ai didi

ios - 如何使用 SegmentControl ios 将数据添加到最后选择的索引和默认索引

转载 作者:行者123 更新时间:2023-11-29 11:51:46 26 4
gpt4 key购买 nike

我正在使用段控制。它包含两个数据。一个数据是默认的,我需要在段中显示默认值。

我正在设置这样的默认数据:

- (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);
}
}
}

我是这样管理索引的:

- (void)segmentedControlValueDidChange:(UISegmentedControl *)segment
{
switch (segment.selectedSegmentIndex)
{
case 0:
NSLog(@"First was selected");
break;

case 1:
NSLog(@"Second was selected");
break;

default:
break;
}
}

我可以在段控制中添加默认值。我可以更改段中的值。假设我选择第二段但是当我移动到下一个 Controller 并回到我的 Controller 段时,首先是索引。我想如果我移动到下一个 Controller 但是当我来到我的 Controller 时。最后选择的段应该在那里做什么

最佳答案

在你的 ViewController.h 中声明了 int 之类的

@property(nonatomic,assign)int selectedIndex;

ViewDidLoad中赋默认值

self.selectedIndex = 0;

CellForRowAtIndexpath中选择索引

-(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];
..
}

并在同一个view controller中添加segment的value change action

- (void)segmentedControlValueDidChange:(UISegmentedControl *)segment
{
switch (segment.selectedSegmentIndex)
{
case 0:
self.selectedIndex = 0;
break;

case 1:
self.selectedIndex = 1;
break;

default:
break;
}
}

从您现有的 Cell 子类中删除以下行

[priceOptionSeg addTarget:self action:@selector(segmentedControlValueDidChange:) forControlEvents: UIControlEventValueChanged];

关于ios - 如何使用 SegmentControl ios 将数据添加到最后选择的索引和默认索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40801495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com