如何阻止执行直到获得用户的标题

时间:2014-06-12 03:11:33

标签: ios objective-c xcode mkmapview cllocationmanager

我有以下代码:

-(void)viewDidLoad
{
   //Set up MapView
   //Set up LocationManager
}

-(IBAction)currentLocation:(id)sender
{
   //Start headings for LocationManager
   //MyCode
     .
     .
     .
   //Need to get the user's heading and continue the code
}

-(void)locationManager:(CLLocationManager *) manager didUpdateHeading:(CLHeading *) newHeading
{
    //Successfully get heading
}

现在,我需要在currentLocation方法中返回标题。我找到了以下选项:

  1. 调度块(我目前不知道如何使用它们,并想知道它们是否真的有必要)。

  2. 继续方法locationManager中的代码。这可以做到,但如果我可以将变量heading带回currentLocation方法并继续我的代码,那会更好。

  3. 我知道有很多关于这个主题的问题,但我无法从这些解决方案中获得任何帮助。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

首先,创建一个属性来存储您的标题 - 在Classname.m文件中的@implementation Classname 之前添加 -

@interface classname ()     // Change classname to *your* class name

@property (retain,nonatomic) CLHeading *currentHeading;

@end

然后在您的viewWillAppear方法中

-(void) viewWillAppear:(BOOL)animated {
   [super viewWillAppear:animated];

   self.theButton.enabled=NO;             //This is the button that triggers your `currentLocation` method

   if (locationManager.headingAvailable) {
       [locationManager startUpdatingHeading];
   }
   else  {
       // Possibly display some alert that heading information is not supported
   }
}

您的didUpdateHeading方法将是 -

-(void)locationManager:(CLLocationManager *) manager didUpdateHeading:(CLHeading *) newHeading
{
    self.currentHeading=newHeading;
    self.theButton.enabled=YES;            //Since we now have a heading value we can enable the button
}

您的currentLocationMethod现在可以在需要标题

时使用self.currentHeading中的值
相关问题