28819
現在、Buttonを押すと値を表示しています。  
表示をButtonを押さずとも更新して表示し、その値をコンソールにログを出力するにはどうすればいいでしょうか？

```

    #import "ViewController.h"
    
    @interface ViewController ()
    
    //アンテナバー
    @property (weak, nonatomic) IBOutlet UILabel *labelAntennaBar;
    //RSSI
    @property (weak, nonatomic) IBOutlet UILabel *labelRssi;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    
    - (IBAction)buttonGetValue:(id)sender {
        self.labelAntennaBar.text  = [NSString stringWithFormat:@"%zd", [self antennaBar]];
        self.labelRssi.text        = [NSString stringWithFormat:@"%zd", [self radioRssi]];
    }
    
    
    #pragma mark - Status bar
    
    - (NSInteger )antennaBar {
        UIApplication *app = [UIApplication sharedApplication];
        NSArray *subviews = [[[app valueForKey:@"statusBar"]
                              valueForKey:@"foregroundView"] subviews];
        NSString *dataNetworkItemView = nil;
    
        for (id subview in subviews) {
            if ([subview isKindOfClass:[NSClassFromString(@"UIStatusBarSignalStrengthItemView") class]]) {
                dataNetworkItemView = subview;
                break;
            }
        }
    
        return  [[dataNetworkItemView valueForKey:@"signalStrengthBars"] intValue];
    }
    
    - (NSInteger )radioRssi {
        UIApplication *app = [UIApplication sharedApplication];
        NSArray *subviews = [[[app valueForKey:@"statusBar"]
                              valueForKey:@"foregroundView"] subviews];
        NSString *dataNetworkItemView = nil;
    
        for (id subview in subviews) {
            if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarSignalStrengthItemView") class]]) {
                dataNetworkItemView = subview;
                break;
            }
        }
    
        return  [[dataNetworkItemView valueForKey:@"signalStrengthRaw"] intValue];
    }
    
    @end
    
```
CC BY-SA 3.0
2016-09-10T07:33:05.310
28799
2016-09-11T05:28:06.570
2016-09-11T05:13:22.803
5519
7590
question
ios objective-c
値に変化が起きたときの値を、コンソールにログを出力するには？
KVO (Key-Value
Observing)を使うのがいいと思います。具体的には下記のように`UIStatusBarSignalStrengthItemView`のインスタンスに対して、それぞれ`signalStrengthBars`と`signalStrengthRaw`のプロパティの変化を監視します。監視は`viewDidLoad`あたりで登録するといいです。

監視対象のプロパティの値が変化すると`-
(void)observeValueForKeyPath:ofObject:change:context:`が呼ばれますのでその中でラベルに値を設定します。

```

    - (void)viewDidLoad {
        [super viewDidLoad];
    
    
        UIApplication *app = [UIApplication sharedApplication];
        NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
        UIView *dataNetworkItemView = nil;
    
        for (id subview in subviews) {
            if ([subview isKindOfClass:[NSClassFromString(@"UIStatusBarSignalStrengthItemView") class]]) {
                dataNetworkItemView = subview;
                break;
            }
        }
    
        [dataNetworkItemView addObserver:self forKeyPath:@"signalStrengthBars" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil];
        [dataNetworkItemView addObserver:self forKeyPath:@"signalStrengthRaw" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil];
    }
    
    
    - (void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary<NSString*, id> *)change context:(nullable void *)context {
        if ([keyPath isEqualToString:@"signalStrengthBars"]) {
            self.labelAntennaBar.text  = [NSString stringWithFormat:@"%@", change[NSKeyValueChangeNewKey]];
        }
        if ([keyPath isEqualToString:@"signalStrengthRaw"]) {
            self.labelRssi.text        = [NSString stringWithFormat:@"%@", change[NSKeyValueChangeNewKey]];
        }
    }
    
```
CC BY-SA 3.0
2016-09-11T05:28:06.570
28819
2016-09-11T05:28:06.570
5519
28799
answer