ios - 首选项捆绑 PSLinkListCell 更改图像

标签 ios xcode jailbreak tweak

我正在尝试为我的首选项包编写代码,这是我在 Xcode (iOSOpenDev) 上创建通知中心小部件时通过勾选“首选项包”框添加的。我有一个包含三个项目的 PSLinkListCell。我希望这三个项目也根据所选选项更改 UIimage View 中的图像。

任何帮助将不胜感激。

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

最佳答案

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

dylankelly.MyStat-preferencesChanged

当用户更改设置时,将通过 Darwin 通知中心发送。因此,您需要注册一个回调,以便在出现此通知时调用。一旦您的代码被加载,您应该做这样的事情(例如,在 MyWidgetViewController.m 中,如果这是管理 ImageView 的地方):

#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 和更新 ImageView 的代码:

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

关于ios - 首选项捆绑 PSLinkListCell 更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16002111/

相关文章:

iOS:将 id 转换为 int

ios - 覆盖 canOpenUrl(_ :) AppDelegate

objective-c - 如何将 CFArrayRef 转换为 NSMutableArray

ios - 在没有按钮 xcode Swift 2 的情况下激活时,Segue 将不会执行

ios - 我的网站导航在某些 iOS 设备上出现故障

objective-c - iOS 中用户定义函数的快速帮助

ios - Swift 4.1 类属性和子字符串前缀为 'some' (Xcode 9.3)

iphone - Titanium.App.Properties 安全吗

iphone - 如何从 Mac 上的终端检查 iPhone 是否越狱?

ios - 使用 IOS 编程连接和断开 USB 电缆与 iPhone