我有一个显示项目数组的UIPickerView
。当我选择一个项目时,它可以传递给我的所有视图控制器。现在我的问题是在移动到不同的view controllers
并返回到UIPIckerView
之后它显示了第一个数组项而不是我选择的项。如何查看所选项目?
//in viewDidLoad()
itemsArray = [[NSArray alloc] initWithObjects:@"5", @"10", @"20", @"30", @"50",@"100", nil];
// if i select 20 and moved to other pages of my controllers and
// return to the UIPickerView i can see the 5 as selected not the 20
有什么建议吗?
答案 0 :(得分:2)
您可以使用NSUserDefaults来保存当前选定的值,
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithInt:albumCount] forKey:@"AlbumCount"];
[defaults synchronize];
用于检索,
albumCount = [defaults integerForKey:@"AlbumCount"];
答案 1 :(得分:1)
您需要将所选值存储在某个位置,既可以作为视图控制器或模型的属性,也可以存储在NSUserDefaults
等中心位置。
然后,当您使用UIPickerView
返回到该视图控制器时,您可以使用
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
设置值。
答案 2 :(得分:1)
根据您要实现的目标,以及您打算如何开启更多视图,可能就像声明@property int currentIndex
,设置currentIndex = indexForSelectedItem
然后使用viewWillAppear
一样简单它会提醒您查看上次选择的内容。
答案 3 :(得分:1)
可以用不同的方式做到
像