草庐IT

iphone - MKMapViewDelegate派生类和委托(delegate)赋值

coder 2024-01-17 原文

这可能更像是一个关于 iOS 的 objective-c 问题,但我看到了一些类似于下面的示例代码,我想更好地理解它们。

@interface MyMapView : MKMapView <MKMapViewDelegate> {
// ivars specific to derived class
}

@property(nonatomic,assign) id<MKMapViewDelegate> delegate;

@end

@implementation MyMapView
- (id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // initialize the ivars specific to this class

        // Q1: Why invoke super on this delegate that's also a property of this class?    
        super.delegate = self;

        zoomLevel = self.visibleMapRect.size.width * self.visibleMapRect.size.height;
    }
    return self;
}
#pragma mark - MKMapViewDelegate methods
// Q2: Why intercept these callbacks, only to invoke the delegate?
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
    if( [delegate respondsToSelector:@selector(mapView:regionWillChangeAnimated:)] )
    {
        [delegate mapView:mapView regionWillChangeAnimated:animated];
    } 
}

@end

我的两个问题是: 1. 为什么要调用 super.delegate 并且只将“委托(delegate)”声明为属性? 2. 为什么拦截所有委托(delegate)调用只是为了将它们转发回委托(delegate)?

我很感激任何见解。

最佳答案

Apple 的文档明确指出您应该避免子类 MKMapView :

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/doc/uid/TP40008205

Although you should not subclass the MKMapView class itself, you can get information about the map view’s behavior by providing a delegate object.

所以我猜这个委托(delegate)“转发”模式是用来不破坏事情的。

我对 MKMapView 使用了一些不同的子类.为了尽量减少破损,我使用了两个类。一个子类 MKMapView并且只需覆盖 init/dealloc 方法并分配/释放 delegate属性到另一个类的实例。另一个类是 NSObject 的子类实现了 MKMapViewDelegate协议(protocol),并将成为真正工作的协议(protocol)。

MyMapView.h

@interface MyMapView : MKMapView
@end

MyMapView.m

// private map delegate class
@interface MapDelegate : NSObject <MKMapViewDelegate>
// instance is not alive longer then MKMapView so use assign to also solve
// problem with circular retain
@property(nonatomic, assign) MKMapView *mapView;
@end

@implementation MapDelegate
@synthesize mapView;

- (id)initWithMapView:(ReportsMapView *)aMapView {
  self = [super init];
  if (self == nil) {
    return nil;
  }

  self.mapView = aMapView;

  return self;
}

// MKMapViewDelegate methods and other stuff goes here

@end

@implementation MyMapView
- (id)init {
  self = [super init];
  if (self == nil) {
    return nil;
  }

  // delegate is a assign property
  self.delegate = [[MapDelegate alloc] initWithMapView:self];

  return self;
}

- (void)dealloc {
  ((MapDelegate *)self.delegate).mapView = nil;
  [self.delegate release];
  self.delegate = nil;

  [super dealloc];
}
@end

mapView MapDelegate 的属性(property)class 不是严格需要的,但如果想对 map View 做一些不是某些 MKMapViewDelegate 的结果的事情,它可能很有用。方法调用、定时器等

关于iphone - MKMapViewDelegate派生类和委托(delegate)赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7586961/

有关iphone - MKMapViewDelegate派生类和委托(delegate)赋值的更多相关文章

  1. ruby - 带括号和 splat 运算符的并行赋值 - 2

    我明白了:x,(y,z)=1,*[2,3]x#=>1y#=>2z#=>nil我想知道为什么z的值为nil。 最佳答案 x,(y,z)=1,*[2,3]右侧的splat*是内联扩展的,所以它等同于:x,(y,z)=1,2,3左边带括号的列表被视为嵌套赋值,所以它等价于:x=1y,z=23被丢弃,而z被分配给nil。 关于ruby-带括号和splat运算符的并行赋值,我们在StackOverflow上找到一个类似的问题: https://stackoverflow

  2. ruby - 变量赋值后的 if 语句 - 有多常见? - 2

    我最近与一位同事讨论了以下Ruby语法:value=ifa==0"foo"elsifa>42"bar"else"fizz"end我个人并没有看到太多这种逻辑,但我的同事指出,这实际上是一种相当普遍的Rubyism。我试着用谷歌搜索这个主题,但没有找到任何文章、页面或SO问题来讨论它,这让我相信这可能是一种非常实际的技术。然而,另一位同事发现语法令人困惑,而是将上面的逻辑写成这样:ifa==0value="foo"elsifa>42value="bar"elsevalue="fizz"end缺点是value=的重复声明和隐式elsenil的丢失,如果我们想使用它的话。这也感觉它与Ruby

  3. ruby - 将属性方法委托(delegate)给父对象 - 2

    我有以下类(class):classAlphabetattr_reader:letter_freqs,:statistic_letterdefinitialize(lang)@lang=langcaselangwhen:en@alphabet=('A'..'Z').to_a@letter_freqs={...}when:ru@alphabet=('А'..'Я').to_a.insert(6,'Ё')@letter_freqs={...}...end@statistic_letter=@letter_freqs.max_by{|k,v|v}[0]endendfoo=Alphabet.n

  4. ruby-on-rails - rspec 模拟对象属性赋值 - 2

    我有一个rspec模拟对象,一个值赋给了属性。我正在努力在我的rspec测试中满足这种期望。只是想知道语法是什么?代码:defcreate@new_campaign=AdCampaign.new(params[:new_campaign])@new_campaign.creationDate="#{Time.now.year}/#{Time.now.mon}/#{Time.now.day}"if@new_campaign.saveflash[:status]="Success"elseflash[:status]="Failed"endend测试it"shouldabletocreat

  5. ruby - 重构条件变量赋值 - 2

    我正在做一个项目。目前我有一个相当大的条件语句,它根据一些输入参数为变量赋值。所以,我有这样的东西。ifsomeconditionx=somevalueelsifanotherconditionx=adifferentvalue...重构它的最佳方法是什么?我希望我最终会得到类似的东西x=somevalueifsomecondition||anothervalueifanothercondition这种事情有规律吗? 最佳答案 只需将赋值放在if之外即可。x=ifsomeconditionsomevalueelsifanotherc

  6. ruby - 以编程方式从字符串派生正则表达式 - 2

    我想输入一个字符串并返回一个可用于描述字符串结构的正则表达式。正则表达式将用于查找更多与第一个结构相同的字符串。这是故意模棱两可的,因为我肯定会漏掉SO社区中的某个人会发现的情况。请发布任何和所有可能的方法来做到这一点。 最佳答案 简单的答案(可能不是您想要的)是:返回输入字符串(正则表达式特殊字符转义)。这始终是与字符串匹配的正则表达式。如果您希望识别某些结构,则必须提供有关您希望识别的结构类型的更多信息。如果没有这些信息,问题就会以模棱两可的方式陈述,并且有许多可能的解决方案。例如,输入字符串'aba'可以描述为'阿巴''阿巴*

  7. Ruby 并行赋值 - 2

    我想测试一个并行赋值的返回值,我写了puts(x,y=1,2),但是不行,打印错误信息:SyntaxError:(irb):74:syntaxerror,unexpected',',expecting')'puts(x,y=1,2)^(irb):74:syntaxerror,unexpected')',expectingend-of-input有什么问题吗? 最佳答案 你有两个问题。puts和(之间的空格防止括号列表被解释为参数列表。一旦你在方法名后放置一个空格,任何argumentlisthastobeoutsidethepare

  8. ruby - 在 Ruby 中为变量赋值时如何避免控制台输出 - 2

    赋值时是否可以避免这种影响:irb(main):584:0>a=true=>trueirb(main):584:0>我有一个代码有很多赋值,当我试图测试它时,由于所有这些返回值,我看不到结果:truefalsetruefalsetruetrue.. 最佳答案 您可以启动irb或附加--noecho选项的控制台。$irb--noecho2.0.0p353:001>true2.0.0p353:002>否则,如果控制台由另一个进程启动,只需设置conf.echo=false$irb2.0.0p353:001>true=>true2.0.0

  9. ruby-on-rails - 在 Rails 中使用 accepts_nested_attributes_for + 批量赋值保护 - 2

    假设你有这个结构:classHouse请注意,Tv的用户是故意不可访问的。所以你有一个三层嵌套的表单,允许你在一个页面上输入房子、房间和电视。这是Controller的创建方法:defcreate@house=House.new(params[:house])if@house.save#...standardstuffelse#...standardstuffendend问题:您究竟如何为每台电视填充user_id(它应该来自current_user.id)?什么是好的做法?这是我在其中看到的catch22。将user_ids直接填充到params散列中(它们嵌套得很深)保存将失败,因

  10. ruby - 了解 Ruby 中赋值和逻辑运算符的优先级 - 2

    在以下示例中,我无法理解Ruby运算符的优先级:x=1&&y=2由于&&的优先级高于=,我的理解是类似于+和*运算符:1+2*3+4解析为1+(2*3)+4它应该等于:x=(1&&y)=2但是,所有Ruby源代码(包括内部语法解析器Ripper)都将其解析为x=(1&&(y=2))为什么?编辑[08.01.2016]让我们关注一个子表达式:1&&y=2根据优先规则,我们应该尝试将其解析为:(1&&y)=2这没有意义,因为=需要特定的LHS(变量、常量、[]数组项等)。但是既然(1&&y)是一个正确的表达式,那么解析器应该如何处理呢?我试过咨询Ruby的parse.y,但它太像意大利面条

随机推荐