我正在获取我的蓝牙le设备(batteryLevel)的电池电量,这是一个浮动的视图。我想把它传递给另一个视图,以便在文本字段中显示它。
视图一中的代码
- (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的值
我做错了什么?我该怎么做?
发布于 2013-05-31 02:51:55
在presentingViewControllers/destinationViewControllers,中使用委托或赋值属性有很多种方法来实现这一点,但由于我不知道你的应用流程,所以我将给你一种方法,它应该适用于你的任何设置。只需更新viewController (即peripheralDelegate )中的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];
}https://stackoverflow.com/questions/16826543
复制相似问题