【发布时间】:2012-04-19 23:50:12
【问题描述】:
我有一个带有 3 个选项的分段控件,用于在 NSUserDefaults 中保存标准/卫星/混合地图。我遇到的一个问题是,每当重新访问 mapSettingsViewController 时,分段控件就会变回第一段。另一个问题是 mapViewController 不会在 viewWillAppear 上重新加载。
我正在使用带有 Storyboard 的 pageCurl 模态转换 - 不确定这是否重要。
这是设置视图控制器(mapSettingsViewController)中保存分段选择的代码:
- (IBAction)changeMapType: (id)sender{
NSInteger index = ((UISegmentedControl*)sender).selectedSegmentIndex;
// Get the shared defaults object.
NSUserDefaults *mapUserPreferences = [NSUserDefaults standardUserDefaults];
if(segmentedControlMapType.selectedSegmentIndex == 0){
// Save the index.
[mapUserPreferences setInteger:index forKey:@"mapViewKey"];
}
else if(segmentedControlMapType.selectedSegmentIndex == 1){
// Save the index.
[mapUserPreferences setInteger:index forKey:@"mapViewKey"];
}
else if(segmentedControlMapType.selectedSegmentIndex == 2){
// Save the index.
[mapUserPreferences setInteger:index forKey:@"mapViewKey"];
}
// Write them to disk
[mapUserPreferences synchronize];
}
然后在第一个视图控制器 (mapViewController) 中显示对用户选择的段的地图基础的更改。我将代码放在 viewWillAppear 中,这样每次视图出现时都会重新加载地图。
-(void) viewWillAppear:(BOOL)animated {
// Get the settings and set the selected index.
NSUserDefaults *mapUserPreferences = [NSUserDefaults standardUserDefaults];
if([mapUserPreferences integerForKey:@"mapViewKey"] == 0) {
mapView.mapType = MKMapTypeStandard;
}
if([mapUserPreferences integerForKey:@"mapViewKey"] == 1) {
mapView.mapType = MKMapTypeSatellite;
}
if([mapUserPreferences integerForKey:@"mapViewKey"] == 2) {
mapView.mapType = MKMapTypeHybrid;
}
}
感谢您的帮助:)
【问题讨论】:
-
我在 IB 中将分段控件更改为未选中并且它可以工作。但是地图仍然没有加载到 mapViewController 上的 viewWillAppear 上。您必须离开视图然后返回然后加载。我想知道页面卷曲过渡是否与 viewWillAppear 没有重新加载地图有关......
标签: objective-c ios nsuserdefaults uisegmentedcontrol