2012-03-23 26 views
4

我正在嘗試使用通知。在我的視圖控制器類中,我有一個bool isFullScreen。當這個布爾的值發生變化時,我想要發送一個通知給所有觀察類。我不太清楚如何去做這件事,因爲BOOL不是一個對象。我怎麼做到這一點?如何發佈到NSNotificationCenter關於布爾狀態?

回答

10
[[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" ]; 
+0

所以任何時候都是全屏顯示值的變化,th通知被髮送了嗎? – Snowman 2012-03-23 14:20:08

+0

不,您必須發送它... – bandejapaisa 2012-03-23 14:21:06

+0

您可能更喜歡在isFullScreen變量上添加KVO。閱讀這個。您註冊爲isFullScreen的KVO觀察員,並且每次更改時,您都會收到通知 – bandejapaisa 2012-03-23 14:21:52

3

只是wrap BOOL中的NSNumber:

[NSNumber numberWithBool:myBool] 
2

你可以在提到的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程序員的基本要素。

+0

所以我有兩個類,FirstClass和SecondClass。 FirstClass具有屬性isFullScreen。如果我希望SecondClass知道對FirstClass.isFullScreen的任何更改,這將如何看待? – Snowman 2012-03-23 14:36:15

+0

SecondClass必須通過Interface Builder連接或通過以編程方式設置的關係來了解FirstClass的實例。 SecondClass將自己註冊爲FirstClass實例的fullScreen屬性更改的觀察者,然後將通知屬性更改。 – 2012-03-23 14:48:57

0

您可以使用NSMutableString *作爲@「YES」和@「NO」來解決這個問題。然後,當你設置字符串@「YES」,一切都在志願觀測會被通知:

[myStringProperty setString:@"YES"]; 

必須使用的NSString了setString value.That是實際調用它。

警告不要使用:

myStringProperty = @"YES"; 

(這不會產生志願通知。)

0

通知的object應張貼通知,而不是一個NSNumber對象。這很重要,以便觀察者可以觀察特定的實例,並且對調用者來說顯而易見的是所有值。通過object傳遞數據是獲取「不響應選擇器」崩潰的簡單方法。更改數據通常在userInfo字典中,但簡單的BOOL通常用兩個通知來處理。在這種情況下,您應該有:

MYViewControlDidEnterFullScreenNotification 
MYViewControlDidExitFullScreenNotification 

object應該是相關的視圖控制器。

請注意,這些通知在時間上非常明確。 之後都發生了變化。您也可以有相應的Will通知。請查看notification listNSWindow,以瞭解如何正確執行此操作的一個很好的示例。請特別注意NSWindowDidEnterFullScreenNotification及其親屬。

您可能也有興趣Some thoughts on NSNotifications

關於KVO的評論很好,通常KVO是一個體面的方法來實現這一點。但通知也很好,比KVO更容易理解和調試。

0

通知的對象字段用來傳遞發送者和不附加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]; 
}