我正在嘗試使用通知。在我的視圖控制器類中,我有一個bool isFullScreen。當這個布爾的值發生變化時,我想要發送一個通知給所有觀察類。我不太清楚如何去做這件事,因爲BOOL不是一個對象。我怎麼做到這一點?如何發佈到NSNotificationCenter關於布爾狀態?
回答
[[NSNotificationCenter defaultCenter] postNotificationName:YourNotificationName object:[NSNumber numberWithBool:isFullScreen]]; //YourNotificationName is a string constant
志願例子:
如果你有志願做它,它會像下面....:
[self addObserver:self forKeyPath:@"isFullScreen" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil];
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString: @"isFullScreen"]) {
BOOL newValue = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
}
}
//and in dealloc
[self removeObserver:self forKeyPath:@"isFullScreen" ];
只是wrap BOOL中的NSNumber:
[NSNumber numberWithBool:myBool]
你可以在提到的bandejapaisa和beryllium中包裝NSNumber中的BOOL。但是,爲了通知觀察者對簡單屬性的更改,最好使用Key Value Observing(KVO),而不是NSNotificationCenter。只要您已經實施了或符合KVC標準的訪問方法,就可以免費獲得KVO。就像這樣:
// In your .h:
@interface YourViewController : UIViewController
@property (getter = isFullScreen) BOOL fullScreen;
@end
// In your .m:
@implementation YourViewController
@synthesize fullScreen;
@end
// In your observer class(es):
// Start observing the viewController for changes to fullScreen (in awakeFromNib, or wherever it makes sense)
[self.viewController addObserver:self forKeyPath:@"fullScreen" options:0 context:NULL];
// This method is called when an observed value changes
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == viewController && [keyPath isEqualToString:@"fullScreen"])
{
if (self.viewController.isFullScreen)
{
// Do whatever you need to do in response to isFullScreen being true
}
else
{
// Do whatever you need to do in response to isFullScreen being false
}
}
}
爲了這個工作,你需要確保你實際上調用setScreen屬性的setter。因此,請始終使用來代替fullScreen = YES
。否則,setter方法不會被調用,並且KVO不會被觸發。您應該閱讀documentation on KVO。瞭解它是成爲一名優秀的iOS程序員的基本要素。
所以我有兩個類,FirstClass和SecondClass。 FirstClass具有屬性isFullScreen。如果我希望SecondClass知道對FirstClass.isFullScreen的任何更改,這將如何看待? – Snowman 2012-03-23 14:36:15
SecondClass必須通過Interface Builder連接或通過以編程方式設置的關係來了解FirstClass的實例。 SecondClass將自己註冊爲FirstClass實例的fullScreen屬性更改的觀察者,然後將通知屬性更改。 – 2012-03-23 14:48:57
您可以使用NSMutableString *作爲@「YES」和@「NO」來解決這個問題。然後,當你設置字符串@「YES」,一切都在志願觀測會被通知:
[myStringProperty setString:@"YES"];
必須使用的NSString了setString value.That是實際調用它。
警告不要使用:
myStringProperty = @"YES";
(這不會產生志願通知。)
通知的object
應張貼通知,而不是一個NSNumber
對象。這很重要,以便觀察者可以觀察特定的實例,並且對調用者來說顯而易見的是所有值。通過object
傳遞數據是獲取「不響應選擇器」崩潰的簡單方法。更改數據通常在userInfo
字典中,但簡單的BOOL通常用兩個通知來處理。在這種情況下,您應該有:
MYViewControlDidEnterFullScreenNotification
MYViewControlDidExitFullScreenNotification
object
應該是相關的視圖控制器。
請注意,這些通知在時間上非常明確。 之後都發生了變化。您也可以有相應的Will
通知。請查看notification list的NSWindow
,以瞭解如何正確執行此操作的一個很好的示例。請特別注意NSWindowDidEnterFullScreenNotification
及其親屬。
您可能也有興趣Some thoughts on NSNotifications。
關於KVO的評論很好,通常KVO是一個體面的方法來實現這一點。但通知也很好,比KVO更容易理解和調試。
通知的對象字段用來傳遞發送者和不附加PARMS
正確的方法是使用USERINFO發送一個密鑰值字典
例如
- (void)postNotificationFullScreen //post notification method and logic
{
NSString *notificationName = @"applicationFullScreen";
NSString *key = @"fullScreen";
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:self.fullScreen] forKey:key];
[[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil userInfo:dictionary];
}
而且要讀取這個
NSString *notificationName = @"applicationFullScreen";
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(useFullScreen:)
name:notificationName
object:nil];
- (void)useFullScreen:(NSNotification *)notification //use notification method and logic
{
NSString *key = @"fullScreen";
NSDictionary *dictionary = [notification userInfo];
BOOL boolValue;
if([dictionary valueForKey:key])
boolValue =[[dictionary valueForKey:key] boolValue];
}
- 1. 如何將布爾值鏈接到UISwitch的開/關狀態?
- 2. 發佈facebook狀態
- 3. Facebook發佈狀態
- 4. 發佈這取決於用戶狀態
- 5. android:如何在facebook上發佈狀態
- 6. 如何顯示布爾狀態爲活動狀態或非活動狀態
- 7. 在Facebook上發佈狀態
- 8. 發佈對象狀態機
- 9. Android發佈狀態意圖
- 10. 發佈後302狀態碼
- 11. Twitter。發佈媒體狀態
- 12. JIRA GreenHopper發佈狀態
- 13. 發佈圖片/情緒到Facebook狀態
- 14. 無狀態組件切換布爾值
- 15. 訂閱布爾狀態使用Rx
- 16. @Output不斷變化的布爾狀態
- 17. 爪哇 - 布爾狀態不改變
- 18. 布爾狀態測試 - 哪個好?
- 19. 基本布爾狀態問題
- 20. 檢查布爾狀態的困難
- 21. 3態布爾?
- 22. 如何將布爾值從ng2服務發佈到MVC WebAPI
- 23. 如何將布爾轉換可空布爾(布爾?)
- 24. 使用cURL和sed輸出基於布爾狀態的圖像
- 25. Python,如何將狀態/ update_with_media發佈到Twitter?
- 26. 如何從asp.net mvc網站發佈狀態到Facebook和Twitter?
- 27. 你如何將狀態發佈到特定的朋友列表?
- 28. 關於伊斯坦布爾巴貝爾,我得不到什麼?
- 29. SQL布爾關係
- 30. 如何在休眠狀態下設置布爾值?
所以任何時候都是全屏顯示值的變化,th通知被髮送了嗎? – Snowman 2012-03-23 14:20:08
不,您必須發送它... – bandejapaisa 2012-03-23 14:21:06
您可能更喜歡在isFullScreen變量上添加KVO。閱讀這個。您註冊爲isFullScreen的KVO觀察員,並且每次更改時,您都會收到通知 – bandejapaisa 2012-03-23 14:21:52