gpt4 book ai didi

ios - 从一个 View Controller 获取数据到另一个

转载 作者:行者123 更新时间:2023-12-01 19:10:38 24 4
gpt4 key购买 nike

我正在获取我的蓝牙 le 设备 (batteryLevel) 的电池电量,它在一个 View 中是 float 的。我想将它传递给另一个 View 以在文本字段中显示它。

View 一中的代码

- (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 这样的值

我想把它放到另一个 View 中显示。

我已经在我放置的 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 的值

我做错了什么,我该怎么做?

最佳答案

有无数种方法可以使用委托(delegate)或在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"];

}

然后在你的另一个 View Controller 中,只需在 viewWillAppear 或其他东西中分配值。
-(void)viewWillAppear:(BOOL)animated
{
float batteryLevel = [[NSUserDefaults standardUserDefaults]floatForKey:@"battery_level"];
self.yourTextField.text = [NSString stringWithFormat:@"%f",batteryLevel];
}

关于ios - 从一个 View Controller 获取数据到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16826543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com