OGeek|极客世界-中国程序员成长平台

标题: ios - Preference Bundle PSLinkListCell 更改图像 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:29
标题: ios - Preference Bundle PSLinkListCell 更改图像

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

任何帮助将不胜感激。

PLIST(仅 PSLinkListCell)

  <dict>
        <key>cell</key>
        <string>SLinkListCell</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>SListItemsController</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>ostNotification</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);



Best Answer-推荐答案


因此,当用户使用 Settings (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 / [email protected]
           break;
        case 2:
           bgView.image = [UIImage imageNamed: @"greenBackground"];
           break;
        case 3:
           bgView.image = [UIImage imageNamed: @"redBackground"];
           break;
        default:
           break;
    }
}

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4