可以访问 NSUserDefualts 但代码不响应键值

发布于 2025-01-04 02:11:30 字数 3219 浏览 3 评论 0原文

(xCode 4.2.1)我有一个实用程序应用程序,在翻转视图中具有一些自定义设置,用于更改主视图控制器中 UIImageView 的外观。这部分我工作得很好。我可以使用 NSLog 打印我的默认值,并且所有操作似乎都正常工作。

我只使用一个键,那就是:“skinPreference”,我用它来简单地切换 MainViewController 中的背景图像。

我的问题是 Flipside ViewDidLoad 函数中的 if 语句。尽管我可以从“skinPreference”中检索正确的值,但它不会响应该 if 语句的前半部分,并且始终执行“else”部分。我在用户设置为默认皮肤首选项的按钮旁边显示一个绿点。当您启动应用程序时,无论我做什么,它似乎总是在 Mahogany 上,但我可以在日志中看到正确的值。

这是导致问题的 if 语句。我需要它来获取“skinPreference”的值,它根据 NSLog 执行此操作,并相应地设置图像。它不起作用。

Flipside.m

- (void)viewDidLoad
{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *skinPref = [defaults valueForKey:@"skinPreference"];

    if (skinPref == @"Classic") {
        NSLog(skinPref, @"@%");
        imgClassicDot.image = [UIImage imageNamed:skinPref];
        imgMahoganyDot.image = [UIImage imageNamed:skinPref];
    } else {
        NSLog(skinPref, @"@%");
        imgClassicDot.image = [UIImage imageNamed:@"dot"];
        imgMahoganyDot.image = [UIImage imageNamed:@"dotGreen"];
    }

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

在我的应用程序 delagate 中,我有以下代码,如果这是应用程序第一次加载,则确保该键具有值:(并且 NSLog 每次都会返回正确的设置)

AppDelegate.m(在 didFinishLaunchingWithOptions 中) )

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *pref = [defaults valueForKey:@"skinPreference"];
        if (pref == nil) {
            NSLog(@"%@ DEFAULTS = %@", [self class], [defaults persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]]);

            [defaults setValue:@"Classic" forKey:@"skinPreference"]; 
            [defaults synchronize];
        }

在我的 Flipside.h 中,我有几个从 2 个按钮触发的自定义方法。 (SkinDidChangeToClassic 和 SkinDidChangeToMahogany)

@protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
- (void)skinDidChangeToClassic;
- (void)skinDidChangeToMahogany;
@end

这是我的 MainViewController.m 中的这 2 个方法

- (void)skinDidChangeToClassic {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setValue:@"Classic" forKey:@"skinPreference"];
    NSString *skinPref = [defaults valueForKey:@"skinPreference"];
    [defaults synchronize];
     skin.image = [UIImage imageNamed:skinPref];

}

- (void)skinDidChangeToMahogany {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setValue:@"Mahogany" forKey:@"skinPreference"];
    NSString *skinPref = [defaults valueForKey:@"skinPreference"];
    [defaults synchronize];
    skin.image = [UIImage imageNamed:skinPref];
}

最后,这是我从 Flipside.m 中触发方法的地方

- (IBAction)setSkinClassic:(id)sender 
{
    imgClassicDot.image = [UIImage imageNamed:@"dotGreen"];
    imgMahoganyDot.image = [UIImage imageNamed:@"dot"];
    [self.delegate skinDidChangeToClassic];
}

- (IBAction)setSkinMahogony:(id)sender 
{
    imgClassicDot.image = [UIImage imageNamed:@"dot"];
    imgMahoganyDot.image = [UIImage imageNamed:@"dotGreen"];
    [self.delegate skinDidChangeToMahogany];
}

我不是专家,所以任何想法都将非常感激。

(xCode 4.2.1) I have a Utility app with a couple of custom settings in the flipside view for changing the appearance of a UIImageView in the Main View Controller. That part I have working perfectly. I can print my defaults with NSLog, and all of the actions seem to be working without fail.

I'm only working with one key, and that is: "skinPreference" which I'm using to simply switch a background image in the MainViewController.

My problem is an if statement in my Flipside ViewDidLoad function. Even though I can retrieve the correct value from "skinPreference", it does not respond to the first half of this if statement, and always executes the "else" portion. I'm displaying a green dot next to the button which the user has set as the default skin preference. It seems to always be on Mahogany when you launch the app, no matter what I do, but I can see the correct value in the log.

Here is the if statement causing the problems. I need it to fetch the valur of "skinPreference," which it does, according to NSLog, and set the images accordingly. It's not working.

Flipside.m

- (void)viewDidLoad
{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *skinPref = [defaults valueForKey:@"skinPreference"];

    if (skinPref == @"Classic") {
        NSLog(skinPref, @"@%");
        imgClassicDot.image = [UIImage imageNamed:skinPref];
        imgMahoganyDot.image = [UIImage imageNamed:skinPref];
    } else {
        NSLog(skinPref, @"@%");
        imgClassicDot.image = [UIImage imageNamed:@"dot"];
        imgMahoganyDot.image = [UIImage imageNamed:@"dotGreen"];
    }

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

In my App delagate, I have the following code, which makes sure the key has a value if it's the very first time the app has loaded: (and the NSLog returns the correct setting every time)

AppDelegate.m (in didFinishLaunchingWithOptions)

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *pref = [defaults valueForKey:@"skinPreference"];
        if (pref == nil) {
            NSLog(@"%@ DEFAULTS = %@", [self class], [defaults persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]]);

            [defaults setValue:@"Classic" forKey:@"skinPreference"]; 
            [defaults synchronize];
        }

In my Flipside.h I have a couple custom methods I trigger from 2 buttons. (SkinDidChangeToClassic, and skinDidChangeToMahogany)

@protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
- (void)skinDidChangeToClassic;
- (void)skinDidChangeToMahogany;
@end

And here are those 2 methods in my MainViewController.m

- (void)skinDidChangeToClassic {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setValue:@"Classic" forKey:@"skinPreference"];
    NSString *skinPref = [defaults valueForKey:@"skinPreference"];
    [defaults synchronize];
     skin.image = [UIImage imageNamed:skinPref];

}

- (void)skinDidChangeToMahogany {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setValue:@"Mahogany" forKey:@"skinPreference"];
    NSString *skinPref = [defaults valueForKey:@"skinPreference"];
    [defaults synchronize];
    skin.image = [UIImage imageNamed:skinPref];
}

And finally, here is where I fire the methods from the flipside.m

- (IBAction)setSkinClassic:(id)sender 
{
    imgClassicDot.image = [UIImage imageNamed:@"dotGreen"];
    imgMahoganyDot.image = [UIImage imageNamed:@"dot"];
    [self.delegate skinDidChangeToClassic];
}

- (IBAction)setSkinMahogony:(id)sender 
{
    imgClassicDot.image = [UIImage imageNamed:@"dot"];
    imgMahoganyDot.image = [UIImage imageNamed:@"dotGreen"];
    [self.delegate skinDidChangeToMahogany];
}

I'm not a guru, so any ideas would be super appreciated.

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

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

发布评论

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

评论(1

诠释孤独 2025-01-11 02:11:30

由于 Objective-C 不使用运算符重载,因此当您执行 if (skinPref == @"Classic") 时,您会检查相等的地址。
但skinPref来自NSUserDefaults,并且具有与静态字符串@“Classic”不同的地址。所以这个说法永远是错误的。

您必须使用

if ([skinPref isEqualToString:@"Classic"])

isEqualToString: 方法检查相等的字符串,而不仅仅是相等的内存地址

since Objective-C does not use operator overloading you are checking for equal addresses when you do if (skinPref == @"Classic").
But skinPref comes from the NSUserDefaults and has a different address than the static string @"Classic". So this statement will always be false.

You have to use

if ([skinPref isEqualToString:@"Classic"])

The isEqualToString: method checks for equal strings and not only for equal memory addresses

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