Question

Im trying to write the code for my preference bundle which i added by ticking the 'preference bundle' box when creating my notification center widget on Xcode (iOSOpenDev). I have a PSLinkListCell with three items inside it. I would like the three items too change the image in a UIimage view depending on the selected choice.

Any help would be much appreciated.

PLIST (only PSLinkListCell)

  <dict>
        <key>cell</key>
        <string>PSLinkListCell</string>
        <key>defaults</key>
        <string>dylankelly.MyStat</string>
        <key>key</key>
        <string>color_pref</string>
        <key>label</key>
        <string>Background Colour</string>
        <key>detail</key>
        <string>PSListItemsController</string>
        <key>validTitles</key>
        <array>
            <string>Blue</string>
            <string>Green</string>
            <string>Red</string>
        </array>
        <key>validValues</key>
        <array>
            <integer>1</integer>
            <integer>2</integer>
            <integer>3</integer>
        </array>
        <key>default</key>
        <integer>1</integer>
        <key>PostNotification</key>
        <string>dylankelly.MyStat-preferencesChanged</string>
    </dict>

UIImage view

UIImage *bg = [[UIImage imageWithContentsOfFile:@"/System/Library/WeeAppPlugins/MyStat.bundle/WeeAppBackground.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:71];
UIImageView *bgView = [[UIImageView alloc] initWithImage:bg];
bgView.frame = CGRectMake(0, 0, 312, 71);
Was it helpful?

Solution

So, what you need is for your widget code to get notified when the user uses Settings (Preferences.app) to change a setting. Based on the way your plist is setup, it looks like a Darwin notification named

dylankelly.MyStat-preferencesChanged

will be sent through the Darwin notification center, when the user has changed the setting. So, you need to register a callback to be invoked when this notification occurs. As soon as your code is loaded, you should do something like this (for example, in MyWidgetViewController.m, if that's where the image views are managed):

#include <notify.h>

...

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                (void*)self, // observer
                                onPreferencesChanged, // callback
                                CFSTR("dylankelly.MyStat-preferencesChanged"), // event name
                                NULL, // object
                                CFNotificationSuspensionBehaviorDeliverImmediately);

where your callback method (put this in the same MyWidgetViewController.m file) would be:

static void onPreferencesChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {

    // since this is a static method, we pass the instance in the observer parameter
    MyWidgetViewController* vc = (MyWidgetViewController*)observer;
    [vc updateImage];
}

and finally, the code to read the preference plist and update your image view:

-(void) updateImage {
    // load the preferences plist file, and read the new color_pref value
    NSDictionary* sharedPrefs = [[NSDictionary alloc] initWithContentsOfFile: PLIST_FILENAME];
    NSNumber* color = (NSNumber*)[sharedPrefs valueForKey: @"color_pref"];
    int colorValue = [color intValue];
    // the integer values correspond to the validValues defined in the 
    //  preference bundle's plist file
    switch (colorValue) {
        case 1:
           bgView.image = [UIImage imageNamed: @"blueBackground"];  // for blueBackground.png / blueBackground@2x.png
           break;
        case 2:
           bgView.image = [UIImage imageNamed: @"greenBackground"];
           break;
        case 3:
           bgView.image = [UIImage imageNamed: @"redBackground"];
           break;
        default:
           break;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top