1

1 つのビューで浮動小数点数である Bluetooth ファイル デバイス (batteryLevel) のバッテリー レベルを取得しています。それを別のビューに渡してテキストフィールドに表示したい。

ビュー 1 のコード

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:    
    (CBCharacteristic *)characteristic error:(NSError *)error
    {[self.testPeripheral readValueForCharacteristic:mycharacteristic];

    char batlevel;
    [characteristic.value getBytes:&batlevel length:1];
    self.batteryLevel = (float)batlevel;

    NSLog(@"level;%f",batteryLevel);}

これにより、80.00000 のような値が得られます

これを別のビューに入れて表示したい。

配置したview2.hファイルで試しました

view1 *t

その後

- (void) batteryIndicatorTimer:(NSTimer *)timer {
    TIBLEUIBatteryBar.progress = t.batteryLevel / 100;
    [t readBattery:[t testPeripheral]];               // Read battery value of keyfob again
    NSLog(@"t.batterylevel:%f",t.batteryLevel);
}

しかし、t.batteryLevelの値が得られません

私は何を間違っていますか、どうすればこれを行うことができますか?

4

1 に答える 1

3

デリゲートを使用したり、presentingViewControllers/destinationViewControllers でプロパティを割り当てたりして、これを実現する方法は無数にありますが、あなたのアプリ フローがわからないので、どのようなセットアップでも機能する方法を紹介します。PeripheralDelegate である viewController の batteryLevel を更新するだけで、その値を NSUserDefaults に保存するだけです。

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:
 (CBCharacteristic *)characteristic error:(NSError *)error
 {
   //Grab your battery value and store in float
   //Then just save to defaults so you can access wherever.. Keep overwriting this value each time you update

   [[NSUserDefaults standardUserDefaults]setFloat:batteryVal forKey:@"battery_level"];

 }

そして、他のviewController内で、viewWillAppearまたは何か内に値を割り当てるだけです。

-(void)viewWillAppear:(BOOL)animated
{
    float batteryLevel = [[NSUserDefaults standardUserDefaults]floatForKey:@"battery_level"];
    self.yourTextField.text = [NSString stringWithFormat:@"%f",batteryLevel];
}
于 2013-05-30T18:51:55.153 に答える