viewDidLoad 时使用 NSUserDefaults 加载存储的注释的字幕

发布于 2024-10-09 22:40:43 字数 752 浏览 8 评论 0原文

基本上,我尝试做的是将副标题(街道地址)保存到注释中。当应用程序终止并再次启动时,我希望此存储的字幕显示在注释的标注中。我做错了什么?我只是无法让它工作...

storeLocation.m

- (void)setCoordinate:(CLLocationCoordinate2D)coor {
                  ............

                    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
                    [userDef setValue:subtitle forKey:@"SavedAddress"];
                    [userDef synchronize];
                    NSLog(@"Stored subtitle");
                }

mainViewController.m

-(void)viewDidLoad {
                NSString *savedAddress = [[NSUserDefaults standardUserDefaults] objectForKey:@"SavedAddress"];
    if (savedAddress) {
    }                
}

非常感谢对此的一些帮助... 提前致谢!

Basically, what I try to do is to save a subtitle(the street address) to an annotation. When the application terminates and starts again I want this stored subtitle to show up in the callout of the annotation. What am I doing wrong? I just can't get it to work...

storeLocation.m

- (void)setCoordinate:(CLLocationCoordinate2D)coor {
                  ............

                    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
                    [userDef setValue:subtitle forKey:@"SavedAddress"];
                    [userDef synchronize];
                    NSLog(@"Stored subtitle");
                }

mainViewController.m

-(void)viewDidLoad {
                NSString *savedAddress = [[NSUserDefaults standardUserDefaults] objectForKey:@"SavedAddress"];
    if (savedAddress) {
    }                
}

Would really appreciate some help with this one... Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

药祭#氼 2024-10-16 22:40:43

尝试使用 srtingForKey: 而不是 objectForKey:

NSString *savedAddress = [[NSUserDefaults standardUserDefaults] stringForKey:@"SavedAddress"];

try srtingForKey: instead of objectForKey:

NSString *savedAddress = [[NSUserDefaults standardUserDefaults] stringForKey:@"SavedAddress"];
做个ˇ局外人 2024-10-16 22:40:43

我用 if 语句解决了这个问题,所以

-(NSString *)subtitle. 

-(NSString *)subtitle{

    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    if ([userDef boolForKey:@"SavedAddress"]) {
        NSString *savedAddress = [[NSUserDefaults standardUserDefaults] stringForKey:@"SavedAddress"];
        return savedAddress;
    }

    else {
        return subtitle;
    }
}

I solved it with an if-statement in the

-(NSString *)subtitle. 

So that:

-(NSString *)subtitle{

    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    if ([userDef boolForKey:@"SavedAddress"]) {
        NSString *savedAddress = [[NSUserDefaults standardUserDefaults] stringForKey:@"SavedAddress"];
        return savedAddress;
    }

    else {
        return subtitle;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文