gpt4 book ai didi

iPhone:UISegmentedControl,如何让导航返回后记住索引?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:49 25 4
gpt4 key购买 nike

我的 tableview Controller 上有一个段。

segmentIndex=0 -> tableView 正在加载 -> 点击。导航到另一个 tableViewController。segmentIndex=1 -> tableView 正在加载 -> 点击。导航到另一个 tableViewController。

切换 TableView 很顺利。问题是导航回来后我需要记住以前的索引值。如何实现?我当前的代码:

- viewWillAppear
self.segment=[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Nominals",@"Coinage",nil]];
[self.segment setFrame:CGRectMake(80, 0, 200, 30)];
[self.segment setSegmentedControlStyle:UISegmentedControlStyleBar];
//self.segment.selectedSegmentIndex=0;
[self.segment addTarget:self action:@selector(segmentValueChaged:) forControlEvents:UIControlEventValueChanged];
[[self navigationItem] setTitleView:self.segment];


-(IBAction)segmentValueChaged:(id)sender
{
switch (self.segment.selectedSegmentIndex)
{
case 0:
{
DBAccess *access=[[DBAccess alloc]init];
self.items=[access returnNominals:self.subCountryID nk:fromPeriod];
[access closeDataBase];
self.tableView.hidden=NO;
[self.tableView reloadData];
self.segmentIndex=0;
break;
}
case 1:
{
DBAccess *access=[[DBAccess alloc]init];
self.coinageArr=[access returnCoinage:fromPeriod period:subCountryID];
self.tableView.hidden=NO;
[self.tableView reloadData];
self.segmentIndex=1;
break;
}
}
}

提前谢谢你们。

编辑:

self.segment=[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Nominals",@"Coinage",nil]];
[self.segment setFrame:CGRectMake(80, 0, 200, 30)];
[self.segment setSegmentedControlStyle:UISegmentedControlStyleBar];
if(!self.segmentIndex)
{
self.segment.selectedSegmentIndex=-1;
}
else
{
self.segment.selectedSegmentIndex=self.segmentIndex;
}

//self.segment.selectedSegmentIndex=self.segmentIndex;
[self.segment addTarget:self action:@selector(segmentValueChaged:) forControlEvents:UIControlEventValueChanged];
[[self navigationItem] setTitleView:self.segment];


-(void)viewWillDisappear:(BOOL)animated
{
switch (self.segment.selectedSegmentIndex)
{
case 0:
{
self.segmentIndex=0;
break;
}
case 1:
{
self.segmentIndex=1;
break;
}
}

}

最佳答案

您将您的值存储在 viewWillDisappear 中,当您返回到您的 Controller viewWillAppear 时,您将设置回您存储的值。所以你还可以做的是,首先将值设置为...比方说-1。如果在您的 viewWillAppear 中值为 -1,则您设置默认值,否则,您使用该值。

- (void)viewWillAppear:(BOOL)animated {
self.segment=[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Nominals",@"Coinage",nil]];
[self.segment setFrame:CGRectMake(80, 0, 200, 30)];
[self.segment setSegmentedControlStyle:UISegmentedControlStyleBar];

if (self->_currentIndex == -1) {
NSInteger myDefaultValue = 0;
self.segment.selectedSegmentIndex=myDefaultValue;
} else {
self.segment.selectedSegmentIndex=self->_currentIndex;
}

[self.segment addTarget:self action:@selector(segmentValueChaged:) forControlEvents:UIControlEventValueChanged];
[[self navigationItem] setTitleView:self.segment];
}

当 Controller 即将消失时,您还必须存储该值。

- (void)viewWillDisappear:(BOOL)animated {
self->_currentIndex = self.segment.selectedSegmentIndex;
}

在界面中定义 NSInteger _currentIndex; 的地方

关于iPhone:UISegmentedControl,如何让导航返回后记住索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10386987/

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