簡體   English   中英

變量更改值時的調用方法

[英]call method when Variable changes value

我在我的應用程序委托.m中定義了一個布爾,然后在視圖控制器.h中定義

extern BOOL usingInternet;

現在,我想在此值更改時調用一個方法,我正在嘗試

[usingInternet addObserver:self forKeyPath:@"myData" options:0 context:@"data"];

但是我收到一個編譯錯誤, Bad receiver type BOOL每當布爾值改變時,如何調用方法?

謝謝

[self addObserver:self forKeyPath:@"usingInternet" options:0 context:nil];

並從中獲得結果

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSLog(@"%@, %@, %@", keyPath, @(usingInternet), change);
}

您不能在BOOL上調用addObserver; 您需要在一個對象上調用它。 您可以將BOOL包裝在NSNumber中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM