停止从 iPhone 用户界面更新位置?

发布于 2024-11-16 21:48:43 字数 388 浏览 4 评论 0原文

是否可以从 iPhone 的 UI 启动/停止位置更新?我需要应用程序显示我的位置,除非我单击“停止”然后再次“开始”。

我似乎无法做到这一点...我的位置已正确显示,我还创建了两个 IBButton 并为每个按钮创建了一个函数,但是,当我单击每个按钮时,我的应用程序崩溃了。我将这些函数放在 viewcontroller.m 下。

我对此有点陌生,所以任何帮助将不胜感激。谢谢!

  • (IBAction)开始更新:(CLLocation *)位置 {

    [位置开始更新位置];

    }

  • (IBAction)停止更新:(CLLocation *)位置 {

    [位置停止更新位置];

    }

Is it possible to start / stop location updates from the UI of the iphone? All I need from the app is to show me my location unless I click "stop" and then "start" again.

I can't seem to be able to do that...I have my location displayed properly, and I also created two IBButtons and created a function for each of them, however, my app crashes when I click on each one of those buttons. I placed those functions under the viewcontroller.m.

I am kind of new to this, so any help would be appreciated. Thanks!

  • (IBAction)startUpdating: (CLLocation *)location
    {

    [location startUpdatingLocation];

    }

  • (IBAction)stopUpdating: (CLLocation *)location
    {

    [location stopUpdatingLocation];

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

り繁华旳梦境 2024-11-23 21:48:43

start/stopUpdatingLocation 是 CLLocationManager 实例方法,而不是 CLLocation 实例方法...因此创建一个 CLLocationManager 实例。

.h

@interface someClass:somesuperclass{
CLLocationManager * locationManager;
BOOL updating;
}
-(IBAction)toggleUpdating:(id)sender;

@end

.m 在视图加载/或初始化周期中的某个位置:

-(void)viewDidLoad{
  [super viewDidLoad];
  locationManager = [[CLLocationManager alloc] init];
 }
 -(void)viewDidUnload{
 [locationManager stopUpdatingLocation];
 [locationManager release];
 [super viewDidUnload];
 }

-(IBAction)toggleUpdating:(id)sender
{
     if(!updating)
     {
         [locationManager startUpdatingLocation];
     }else{
         [locationManager stopUpdatingLocation];
     }
     updating = !updating;
}

上面的操作也永远不会起作用,因为操作中冒号后面的东西将是发送操作的对象,在您的情况下是 UIButton 。

start/stopUpdatingLocation are CLLocationManager instance methods, rather than CLLocation instance methods... so create a CLLocationManager instance.

.h

@interface someClass:somesuperclass{
CLLocationManager * locationManager;
BOOL updating;
}
-(IBAction)toggleUpdating:(id)sender;

@end

.m somewhere in the view load/ or init cycle:

-(void)viewDidLoad{
  [super viewDidLoad];
  locationManager = [[CLLocationManager alloc] init];
 }
 -(void)viewDidUnload{
 [locationManager stopUpdatingLocation];
 [locationManager release];
 [super viewDidUnload];
 }

-(IBAction)toggleUpdating:(id)sender
{
     if(!updating)
     {
         [locationManager startUpdatingLocation];
     }else{
         [locationManager stopUpdatingLocation];
     }
     updating = !updating;
}

also your action above will never work, because the thing after a colon in an action will be the object that sent the action, a UIButton in your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文