- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
View Controller :
@implementation PaiLifeViewController
@synthesize detail = _detail;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *i = [UIImage imageNamed:@"menu_patcode_normal"];
UIButton *c1 = [[UIButton alloc] init];
c1.tag = 11;
UIButton *c2 = [[UIButton alloc] init];
c2.tag = 12;
UIButton *c3 = [[UIButton alloc] init];
c3.tag = 13;
UIButton *c4 = [[UIButton alloc] init];
c4.tag = 21;
UIButton *c5 = [[UIButton alloc] init];
c5.tag = 22;
UIButton *c6 = [[UIButton alloc] init];
c6.tag = 23;
UIButton *c7 = [[UIButton alloc] init];
c7.tag = 31;
UIButton *c8 = [[UIButton alloc] init];
c8.tag = 32;
UIButton *c9 = [[UIButton alloc] init];
c9.tag = 33;
[c1 setBackgroundImage:i forState:UIControlStateNormal];
[c2 setBackgroundImage:i forState:UIControlStateNormal];
[c3 setBackgroundImage:i forState:UIControlStateNormal];
[c4 setBackgroundImage:i forState:UIControlStateNormal];
[c5 setBackgroundImage:i forState:UIControlStateNormal];
[c6 setBackgroundImage:i forState:UIControlStateNormal];
[c7 setBackgroundImage:i forState:UIControlStateNormal];
[c8 setBackgroundImage:i forState:UIControlStateNormal];
[c9 setBackgroundImage:i forState:UIControlStateNormal];
NSArray *c = [[NSArray alloc] initWithObjects:c1,c2,c3,c4,c5,c6,c7,c8,c9, nil];
_detail= [[PaiLifePageDetail alloc] initWithDataSource:c pageIndex:1];
for (UIButton *b in c)
{
[b addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
}
[self.view addSubview:_detail];
}
- (void) clicked : (UIButton *) button
{
NSLog(@"button = %i", button.tag);
}
@end
自定义 View :
@implementation PaiLifePageDetail
#define MARGINTOTOP 120.0
#define MARGINTOLEFT 20.0
#define WIDTH 80.0
#define HEIGHT 60.0
- (id) initWithDataSource : (NSArray *) dataSource pageIndex : (int) pageIndex;
{
self = [super init];
if (self)
{
if (dataSource)
{
for (UIButton *button in dataSource)
{
//page 1 line 1
if (pageIndex == 1)
{
if (button.tag == 11)
{
button.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP, WIDTH * 2 + 20, HEIGHT);
[self addSubview:button];
}
else if (button.tag == 12)
{
button.frame = CGRectMake(MARGINTOLEFT + WIDTH * 2 + 20 * 2, MARGINTOTOP, WIDTH, HEIGHT);
[self addSubview:button];
}
}
//not page 1 line 1
else
{
if ( button.tag == 11 )
{
button.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP, WIDTH, HEIGHT);
[self addSubview:button];
}
else if ( button.tag == 12 )
{
button.frame = CGRectMake(MARGINTOLEFT + WIDTH + 20, MARGINTOTOP, WIDTH, HEIGHT);
[self addSubview:button];
}
else if ( button.tag == 13 )
{
button.frame = CGRectMake(MARGINTOLEFT + (WIDTH + 20) * 2, MARGINTOTOP, WIDTH, HEIGHT);
[self addSubview:button];
}
}
// line 2
if ( button.tag == 21 )
{
button.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP + HEIGHT + 20, WIDTH, HEIGHT);
[self addSubview:button];
}
else if ( button.tag == 22 )
{
button.frame = CGRectMake(MARGINTOLEFT + WIDTH + 20, MARGINTOTOP + HEIGHT + 20, WIDTH, HEIGHT);
[self addSubview:button];
}
else if ( button.tag == 23 )
{
button.frame = CGRectMake(MARGINTOLEFT + (WIDTH + 20) * 2, MARGINTOTOP + HEIGHT + 20, WIDTH, HEIGHT);
[self addSubview:button];
}
// line 3
if ( button.tag == 31 )
{
button.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP + (HEIGHT + 20) * 2, WIDTH, HEIGHT);
[self addSubview:button];
}
else if ( button.tag == 32 )
{
button.frame = CGRectMake(MARGINTOLEFT + WIDTH + 20, MARGINTOTOP + (HEIGHT + 20) * 2, WIDTH, HEIGHT);
[self addSubview:button];
}
else if ( button.tag == 33 )
{
button.frame = CGRectMake(MARGINTOLEFT + (WIDTH + 20) * 2, MARGINTOTOP + (HEIGHT + 20) * 2, WIDTH, HEIGHT);
[self addSubview:button];
}
}
}
}
return self;
}
@end
但是当我点击这些按钮时,它们根本不起作用,请帮助我解决这个问题,非常感谢。
最佳答案
这可能是触摸处理事件问题。 IE。触摸永远不会触及您的自定义按钮。要对此进行调试:
UIGestureRecognizer
按钮的 super View - 它们可能会干扰触摸操作。 关于ios - UIButton addTarget 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15071628/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!