ios - Preference Bundle PSLinkListCell 更改图像
<p><p>我正在尝试为我在 Xcode (iOSOpenDev) 上创建通知中心小部件时通过勾选“首选项包”框添加的首选项包编写代码。我有一个 <code>PSLinkListCell</code> 里面有三个项目。我希望这三个项目也根据所选选项更改 <code>UIimage</code>View 中的图像。 </p>
<p>任何帮助将不胜感激。 </p>
<p>PLIST(仅 PSLinkListCell)</p>
<pre class="lang-xml prettyprint-override"><code><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>
</code></pre>
<p>UIImageView </p>
<pre class="lang-objc prettyprint-override"><code>UIImage *bg = [ stretchableImageWithLeftCapWidth:5 topCapHeight:71];
UIImageView *bgView = [ initWithImage:bg];
bgView.frame = CGRectMake(0, 0, 312, 71);
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>因此,当用户使用 <strong>Settings</strong> (Preferences.app) 更改设置时,您需要让您的小部件 <strong>代码</strong> 得到通知。根据您的 plist 设置方式,它看起来像 <a href="https://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFNotificationCenterRef/Reference/reference.html" rel="noreferrer noopener nofollow">Darwin notification</a>命名</p>
<pre><code>dylankelly.MyStat-preferencesChanged
</code></pre>
<p>当用户更改设置时,将通过 Darwin 通知中心发送。因此,您需要注册一个回调,以便在发生此通知时调用。一旦你的代码被加载,你应该做这样的事情(例如,在 MyWidgetViewController.m 中,如果这是管理 ImageView 的地方):</p>
<pre><code>#include <notify.h>
...
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
(void*)self, // observer
onPreferencesChanged, // callback
CFSTR("dylankelly.MyStat-preferencesChanged"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
</code></pre>
<p>你的回调方法(把它放在同一个 MyWidgetViewController.m 文件中)将是:</p>
<pre><code>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;
;
}
</code></pre>
<p>最后,读取偏好 plist 并更新 ImageView 的代码:</p>
<pre><code>-(void) updateImage {
// load the preferences plist file, and read the new color_pref value
NSDictionary* sharedPrefs = [ initWithContentsOfFile: PLIST_FILENAME];
NSNumber* color = (NSNumber*);
int colorValue = ;
// the integer values correspond to the validValues defined in the
//preference bundle's plist file
switch (colorValue) {
case 1:
bgView.image = ;// for blueBackground.png / [email protected]
break;
case 2:
bgView.image = ;
break;
case 3:
bgView.image = ;
break;
default:
break;
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - Preference Bundle PSLinkListCell 更改图像,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/16002111/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/16002111/
</a>
</p>
页:
[1]