我有一个带有 3 个选项的分段控件,用于在 NSUserDefaults 中保存标准/卫星/混合 map 。我遇到的一个问题是,每当重新访问 mapSettingsViewController 时,分段控件都会变回第一段。另一个问题是 mapViewController 不会在 viewWillAppear 上重新加载。
我在 Storyboarding 中使用 pageCurl 模式转换 - 不确定这是否重要。
这是设置 View Controller (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];
}
然后在第一个 View Controller (mapViewController)中根据用户选择的段显示对 map 的更改。我将代码放在 viewWillAppear 中,以便在每次出现 View 时重新加载 map 。
-(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;
}
}
感谢您的帮助:)
最佳答案
要检查,当 modalView (pageCurl) 被关闭时,只需将委托(delegate)方法添加到您的 pageCurl Controller :
viewWillDisappear:
然后在那里添加 map 更新逻辑。
关于objective-c - 使用 UISegmentedControl 通过 NSUserDefaults 保存 mapKit 首选项 - 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10230310/