设置CLLocationManager的委托(两种不同的方式,它们是否相等?)

编程入门 行业动态 更新时间:2024-10-25 10:23:03
本文介绍了设置CLLocationManager的委托(两种不同的方式,它们是否相等?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

- (id)initWithNibName :(在这里我使用CLLocationManager编写一个教程) NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if(self) { locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:self]; [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; [locationManager startUpdatingLocation]; } return self; }

设置代理:

[locationManager setDelegate:self];

在另一个教程中,我将代理设置为头文件:

@interface MyViewController:UIViewController< CLLocationManagerDelegate>

他们是否相等?有什么区别(如果有的话)?

解决方案

如果要实现并获取回调,您将需要两者CLLocationManger的代表

您可以在标题中指定这个。

@界面MyViewController:UIViewController< CLLocationManagerDelegate>

这告诉Xcode MyViewController将实现CLLocationManagerDelegate方法。如果有非可选代理方法,xcode将提醒您实现它们。

以下行

[locationManager setDelegate:self];

你告诉你的CLLocationManager(locationManager)的实例,MyViewController(self)将是委托,它应该调用所有实现的CLLocationManagerDelegate方法(如果需要)

Working on a tutorial with CLLocationManager where I set the delegate within the init method:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:self]; [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; [locationManager startUpdatingLocation]; } return self; }

Set the delegate:

[locationManager setDelegate:self];

In another tutorial I set the delegate in a header file:

@interface MyViewController : UIViewController <CLLocationManagerDelegate>

Are they equal? Whats the difference (if there is one)?

解决方案

You will need both of them if you want to implement and get callbacks from the delegate of CLLocationManger

You specify this in the header

@interface MyViewController : UIViewController <CLLocationManagerDelegate>

This tells xcode that MyViewController will implement the CLLocationManagerDelegate methods. If there are non-optional delegate methods, xcode will remind you to implement them.

With the line below

[locationManager setDelegate:self];

You tell your instance of CLLocationManager (locationManager) that MyViewController (self) will be the delegate and it should call all the implemented CLLocationManagerDelegate methods that you have implemented (if needed)

更多推荐

设置CLLocationManager的委托(两种不同的方式,它们是否相等?)

本文发布于:2023-10-26 21:54:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531456.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两种   方式   CLLocationManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!