Preference Bundle PSLinkListCell更改图像

时间:2013-04-14 17:34:38

标签: ios xcode jailbreak tweak

我正在尝试编写我的偏好包的代码,我在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);

1 个答案:

答案 0 :(得分:1)

因此,当用户使用设置(Preferences.app)更改设置时,您需要使用代码来获取通知。根据您的plist设置方式,它看起来像Darwin notification名为

dylankelly.MyStat-preferencesChanged
当用户更改设置时,

将通过达尔文通知中心发送。因此,您需要注册在发生此通知时要调用的回调。加载代码后,您应该执行类似的操作(例如,在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;
    }
}