- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我已经为带有按钮的自定义标注创建了一个 View 。单击 Mapview 上的任何注释时会显示此自定义标注。但是当我点击自定义标注内的按钮时没有任何反应。
#import <UIKit/UIKit.h>
@interface TestView : UIView
@end
#import "TestView.h"
@implementation TestView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(IBAction)showMessage:(id)sender
{
NSLog(@"Test");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
if(![view.annotation isKindOfClass:[MKUserLocation class]]) {
CGRect rect = CGRectMake(0,0,268,140);
TestView *testview = [[TestView alloc] initWithFrame:rect];
testview = [self loadNibNamed:@"TestView" ofClass:[TestView class]];
[view addSubview:testview];
}
}
View 是使用 loadNibNamed 方法加载的。
- (id)loadNibNamed:(NSString *)nibName ofClass:(Class)objClass {
if (nibName && objClass) {
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName owner:nil options:nil];
for (id currentObject in objects ){
if ([currentObject isKindOfClass:objClass])
return currentObject;
}
}
return nil;
}
我已经检查了 View 和 Button 的 userInteractionEnabled 属性,两者都设置为 true。
还将 showMessage 映射到按钮的 Touch Up Inside 事件。
关于我如何识别这个问题有什么建议吗?
最佳答案
尝试使用下面的静态方法获取TestView
+ (TestView *)getNewTestView {
TestView *view = nil;
NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"TestView" owner:nil options:nil];
for (NSObject *obj in xib) {
if ([obj isKindOfClass:[TestView class]]) {
view = (TestView *)obj;
}
}
return view;
然后,要获取对象的实例,请使用:
TestView *testView = [TestView getNewTestView];
[self.view addSubview:testView];
关于ios - UIView 中的 Button 没有调用 IBAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20212566/
我遇到过这个 html: 上面的html和这个有什么区别: 最佳答案 来自MDN page on the tag : 对于 type 的属性标签,可能的值是: 提交:按钮将表单数据提交给服务器
Button button= (Button) findViewbyID(R.id.button); 和 Button button = new Button(this); 有什么区别? 最佳答案 有
我是一名优秀的程序员,十分优秀!