objective-c - 使用 UISegmentedControl 通过 NSUserDefaults 保存 mapKit 首选项 - 无法正常工作

标签 objective-c ios nsuserdefaults uisegmentedcontrol

我有一个带有 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/

相关文章:

objective-c - 调用 NSDictionary 中所有包含 JSON 对象的 Key 时出错

ios - 一个对象有多个委托(delegate)? (跟进)

ios - tableviewcell 中的图像不会立即出现

ios - View 中的中心图像

iphone - 在应用程序开始时强制采用横向模式,但允许稍后更改方向

ios - 尝试在 NSUserDefaults 中存储 nsdictionaries 数组时出错

arrays - 使用结构体、数组和用户默认值 (Swift)

ios - NSMutableDictionary:发送到不可变对象(immutable对象)的变异方法

objective-c - NSClassFromString 返回一个没有正确响应 isSubClassOfClass 的 Class 对象

iphone - 数据库层设计