• <bdo id='PGkoi'></bdo><ul id='PGkoi'></ul>

      <small id='PGkoi'></small><noframes id='PGkoi'>

        <tfoot id='PGkoi'></tfoot>

        <legend id='PGkoi'><style id='PGkoi'><dir id='PGkoi'><q id='PGkoi'></q></dir></style></legend>
        <i id='PGkoi'><tr id='PGkoi'><dt id='PGkoi'><q id='PGkoi'><span id='PGkoi'><b id='PGkoi'><form id='PGkoi'><ins id='PGkoi'></ins><ul id='PGkoi'></ul><sub id='PGkoi'></sub></form><legend id='PGkoi'></legend><bdo id='PGkoi'><pre id='PGkoi'><center id='PGkoi'></center></pre></bdo></b><th id='PGkoi'></th></span></q></dt></tr></i><div id='PGkoi'><tfoot id='PGkoi'></tfoot><dl id='PGkoi'><fieldset id='PGkoi'></fieldset></dl></div>

        子类化 UIButton 但无法访问我的属性

        时间:2023-07-08
        • <small id='oaMAF'></small><noframes id='oaMAF'>

        • <legend id='oaMAF'><style id='oaMAF'><dir id='oaMAF'><q id='oaMAF'></q></dir></style></legend>

              <tbody id='oaMAF'></tbody>

              <i id='oaMAF'><tr id='oaMAF'><dt id='oaMAF'><q id='oaMAF'><span id='oaMAF'><b id='oaMAF'><form id='oaMAF'><ins id='oaMAF'></ins><ul id='oaMAF'></ul><sub id='oaMAF'></sub></form><legend id='oaMAF'></legend><bdo id='oaMAF'><pre id='oaMAF'><center id='oaMAF'></center></pre></bdo></b><th id='oaMAF'></th></span></q></dt></tr></i><div id='oaMAF'><tfoot id='oaMAF'></tfoot><dl id='oaMAF'><fieldset id='oaMAF'></fieldset></dl></div>
              1. <tfoot id='oaMAF'></tfoot>
                  <bdo id='oaMAF'></bdo><ul id='oaMAF'></ul>
                  本文介绍了子类化 UIButton 但无法访问我的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我创建了一个 UIButton 的子类:

                  I've created a sub class of UIButton:

                  //
                  //  DetailButton.h
                  #import <Foundation/Foundation.h>
                  #import <MapKit/MapKit.h>
                  
                  @interface MyDetailButton : UIButton {
                      NSObject *annotation;
                  }
                  
                  @property (nonatomic, retain) NSObject *annotation;
                  
                  
                  @end
                  
                  //
                  //  DetailButton.m
                  //
                  
                  
                  #import "MyDetailButton.h"
                  
                  
                  @implementation MyDetailButton
                  
                  
                  @synthesize annotation;
                  
                  @end
                  

                  我想我可以通过执行以下操作来创建这个对象并设置注释对象:

                  I figured that I can then create this object and set the annotation object by doing the following:

                  MyDetailButton* rightButton = [MyDetailButton buttonWithType:UIButtonTypeDetailDisclosure];
                  rightButton.annotation = localAnnotation;
                  

                  localAnnotation 是一个 NSObject 但它实际上是一个 MKAnnotation.我不明白为什么这不起作用,但在运行时我收到此错误:

                  localAnnotation is an NSObject but it is really an MKAnnotation. I can't see why this doesn't work but at runtime I get this error:

                   2010-05-27 10:37:29.214 DonorMapProto1[5241:207] *** -[UIButton annotation]: unrecognized selector sent to instance 0x445a190
                  2010-05-27 10:37:29.215 DonorMapProto1[5241:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIButton annotation]: unrecognized selector sent to instance 0x445a190'
                  

                  '

                  我看不出它为什么要查看 UIButton,因为我已经对它进行了子类化,所以它应该查看 MyDetailButton 类来设置该注释属性.我是否错过了一些非常明显的事情.感觉是这样的:)

                  I can't see why it's even looking at UIButton because I've subclassed that so it should be looking at the MyDetailButton class to set that annotation property. Have I missed something really obvious. It feels like it :)

                  提前感谢您提供的任何帮助

                  Thanks in advance for any help you can provide

                  罗斯

                  推荐答案

                  UIButton 是一个类簇,这意味着苹果的 buttonWithType: 实现大概是这样的:

                  UIButton is a class cluster, which implies that Apple's implementation of buttonWithType: probably looks something like this:

                  +(id)buttonWithType:(UIButtonType)t {
                    switch (t) {
                      case UIButtonTypeDetailDisclosure:
                        return [[[PrivateDetailDisclosureButtonClass alloc] init] autorelease];
                      case ...
                    }
                  }
                  

                  因此,当您调用 [MyDetailButton buttonWithType:UIButtonTypeDetailDisclosure]; 时,您不会得到 MyDetailButton 的实例,而是会得到 PrivateDetailDisclosureButtonClass (或任何苹果实际称呼的).

                  So when you call [MyDetailButton buttonWithType:UIButtonTypeDetailDisclosure]; you don't get an instance of MyDetailButton, you get an instance of PrivateDetailDisclosureButtonClass (or whatever Apple actually calls it).

                  但是请注意,如果您使用 UIButtonTypeCustom 调用它,您可以buttonWithType 实例化一个子类(至少在模拟器运行v3.0):

                  Note, however, that you can get buttonWithType to instantiate a subclass if you call it with UIButtonTypeCustom (At least in the simulator running v3.0):

                  // LGButton is a straightforward subclass of UIButton
                  LGButton *testBtn = [LGButton buttonWithType:UIButtonTypeCustom];
                  LGButton *testBtn2 = [LGButton buttonWithType:UIButtonTypeDetailDisclosure];
                  NSLog(@"testBtn: %@, testBtn2: %@", [testBtn class], [testBtn2 class]);
                  // Output: testBtn: LGButton, testBtn2: UIButton
                  

                  这篇关于子类化 UIButton 但无法访问我的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在一个圆圈内对齐 UIButtons? 下一篇:Swift - UIButton 覆盖 setSelected

                  相关文章

                  <legend id='NzruR'><style id='NzruR'><dir id='NzruR'><q id='NzruR'></q></dir></style></legend>
                    <bdo id='NzruR'></bdo><ul id='NzruR'></ul>
                  1. <i id='NzruR'><tr id='NzruR'><dt id='NzruR'><q id='NzruR'><span id='NzruR'><b id='NzruR'><form id='NzruR'><ins id='NzruR'></ins><ul id='NzruR'></ul><sub id='NzruR'></sub></form><legend id='NzruR'></legend><bdo id='NzruR'><pre id='NzruR'><center id='NzruR'></center></pre></bdo></b><th id='NzruR'></th></span></q></dt></tr></i><div id='NzruR'><tfoot id='NzruR'></tfoot><dl id='NzruR'><fieldset id='NzruR'></fieldset></dl></div>
                  2. <tfoot id='NzruR'></tfoot>
                  3. <small id='NzruR'></small><noframes id='NzruR'>