【问题标题】:Custom Button of type UIButtonTypeInfoLightUIButtonTypeInfoLight 类型的自定义按钮
【发布时间】:2023-03-20 19:05:01
【问题描述】:

我创建了一个自定义按钮,为什么我需要在@selector 中作为参数传递。自定义按钮是 annotationView 的一部分。在 CustomButton 中,我将其作为属性 UIButtonType,但在输出中什么也没有出现。输出是一个没有任何内容的按钮,当我想要打开视图控制器时,当我在注释内部进行修饰时消失。

这是CustomButton.h中的代码

@interface CustomButton : UIButton{

}

@property (nonatomic, strong)NSString * name;
@property (nonatomic, assign)UIButtonType  typeButton;
@end

在 CustomButton.m 中

@implementation CustomButton.h

@synthesize name;
@synthesize buttonType;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

         self.typeButton = UIButtonTypeInfoLight;

    }
return self;
}

在 MapViewController.m 中

 -(void)loadDetailListViewController: (CustomButton *)aName{ 

             //I want to open a viewController 
             //.......
  }

 - (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id 
 <MKAnnotation>)annotation
  {
       //.......  

       MKPinAnnotationView *annotationView = (MKPinAnnotationView*) [mapView 
                dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];


       annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                  reuseIdentifier:AnnotationIdentifier];

     //.........

     CustomButton *rightButton = [CustomButton buttonWithType:UIButtonTypeCustom];
     [rightButton setName:[NSString stringWithFormat:@"%@", self.nameTable]];

     [rightButton addTarget:self action: @selector(loadDetailListViewController:) 
                                forControlEvents:UIControlEventTouchUpInside];

     annotationView.rightCalloutAccessoryView = rightButton;
     annotationView.canShowCallout = YES;
     annotationView.draggable = YES;
     return annotationView;
   }

为什么当我触摸里面的注解消失了?

【问题讨论】:

  • 如果您只想在 loadDetailListViewController 方法中访问一些与注解相关的信息,您可以使用常规 UIButton 并使用 calloutAccessoryControlTapped 委托方法,您可以在其中访问 view.annotation。跨度>
  • 我该怎么做?请给我举个例子?
  • @AnnaKarenina 非常感谢,现在一切正常!

标签: ios uibutton selector mkannotationview


【解决方案1】:

请务必遵循文档说明:

buttonWithType:创建并返回指定的新按钮 输入。

  • (id)buttonWithType:(UIButtonType)buttonType

参数

buttonType 按钮类型。有关可能的值,请参见 UIButtonType。

返回值

一个新创建的按钮。

讨论这个方法是一个方便的构造函数,用于创建 具有特定配置的按钮对象。 它是 UIButton 的子类, 此方法不会返回您的子类的实例。如果你愿意 要创建特定子类的实例,您必须分配/初始化 直接按钮。

当创建一个自定义按钮时——该按钮的类型为 UIButtonTypeCustom——按钮的框架设置为 (0, 0, 0, 0) 原来。在将按钮添加到界面之前,您应该 将框架更新为更合适的值。

当您在buttonWithType: 上使用UIButtonTypeCustom 时,您的代码会以某种方式工作,如果有人更改它(例如更改为UIButtonTypeRoundedRect)会发生不好的事情。

由于您没有在代码中的任何地方使用CustomButton -initWithFrame:,但提供了它的实现,我建议您将其用作所需的初始化程序,这很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 2018-07-20
    相关资源
    最近更新 更多