2

我试图为我的偏好包编写代码,我在 Xcode (iOSOpenDev) 上创建通知中心小部件时勾选了“偏好包”框。我有一个PSLinkListCell里面有三件物品。我希望这三个项目也UIimage根据所选选项更改视图中的图像。

任何帮助将非常感激。

PLIST(仅限 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 视图

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);
4

1 回答 1

1

因此,您需要让您的小部件代码在用户使用设置(Preferences.app)更改设置时得到通知。根据您的 plist 设置方式,它看起来像一个名为Darwin 的通知

dylankelly.MyStat-preferencesChanged

当用户更改设置时,将通过 Darwin 通知中心发送。因此,您需要注册一个回调,以便在发生此通知时调用。加载代码后,您应该执行以下操作(例如,在 MyWidgetViewController.m 中,如果这是管理图像视图的位置):

#include <notify.h>

...

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

您的回调方法(将其放在同一个 MyWidgetViewController.m 文件中)将是:

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];
}

最后,读取首选项 plist 并更新图像视图的代码:

-(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;
    }
}
于 2013-04-16T01:54:19.760 回答