2013-05-30 4 views
1

하나의보기에서 float 인 블루투스 장치 (batteryLevel)의 배터리 잔량이 나타납니다. 텍스트 필드에 표시하기 위해 다른보기로 전달하고 싶습니다. 이 날 80.00000하나의보기 컨트롤러에서 다른보기 컨트롤러로 데이터 가져 오기

같은 값을 제공보기 하나

- (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);} 

에서

코드는 내가 표시하는 다른보기에 이것을 넣고 싶다.

은 내가

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

에 대한 값을 얻을하지 않습니다 view2.h 파일에 시도 나는 무엇이다 잘못하고 어떻게 할 수 있습니까?

+0

이것은 실제로 CoreBluetooth와 관련이 없으며 아키텍처의 문제입니다. 통신 클래스를 만들어 공유 인스턴스로 설정하십시오. – Larme

답변

3

delegate를 사용하거나 presentController/destinationViewControllers에서 속성을 할당하는 방법은 무수히 많지만 앱 흐름을 알지 못하기 때문에 설치에 관계없이 어떤 방식 으로든 작동해야합니다. 그냥 peripheralDelegate 다음 단순히 NSUserDefaults에 값을 저장 귀하의 ViewController에 batteryLevel를 업데이트

- (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]; 
} 
관련 문제