从另一个类iOS获取动态bool变量

时间:2015-08-14 16:36:27

标签: ios objective-c core-location

我有位置跟踪类和另一个带有列表的类。我需要在输入listView时检查locationService是否正在运行。在locationService类中,我添加了boolean来跟踪服务的启动和停止。但是我无法从listView类中跟踪这个布尔变量。

这是我的代码,我现在正在做什么,但它只需要locationService类的初始值

//listView.m

@property (strong,nonatomic) LocationTracker * trackerClass;


- (void)viewWillAppear:(BOOL)animated{
  self.trackerClass = [[LocationTracker alloc] init];

  if([self.trackerClass getTrackerStatus]==1)
    NSLog(@"LOCATION SERVICE STATUS IS RUNNING");
   else 
    NSLog(@"LOCATION SERVICE STATUS IS STOPPED");
}


//locationService.h

     @property (nonatomic, readwrite) int trackerStatus;
     - (int)getTrackerStatus;

//locationService.m

- (void)stopLocationTracking {_trackerStatus=0;}
- (void)startLocationTracking {_trackerStatus=1;}


- (int)getTrackerStatus{
    return _trackerStatus;
}

提前谢谢你!

2 个答案:

答案 0 :(得分:0)

尝试将此布尔变量设置为局部变量,并创建一个简单的静态getter来获取此bool。您的LocationTracker将更改变量的值,您可以从任何地方获取它。

@implementation LocationTracker

BOOL isStart;

//Dont forget to delclear this methode in the .h file
+(BOOL)isStart{
    return isStart;
}

your code...

您的应用中的任何位置只需导入您的calss和wirte:

BOOL isStart = [LocationTracker isStart];
//The getting vaiable should be the updated bool in you LocationTracker class

答案 1 :(得分:-1)

尝试删除

- (int)getTrackerStatus{
    return _trackerStatus;
}