设置CLLocationManager的委托(两种不同的方式,它们是否相等?) [英] Set delegate of CLLocationManager (Two different ways, are they equal?)

查看:180
本文介绍了设置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的委托(两种不同的方式,它们是否相等?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆