0

我正在尝试以编程方式添加多个子视图,但这些子视图中的按钮不起作用。这些按钮也以编程方式添加为子视图。

这是代码,希望能更多地解释我的意思。

self = [super initWithFrame:frame];
if (self) {
    // Initialization code
}

[self setBackgroundColor:[UIColor clearColor]];

// UIView container of selected image with comment button.
UIView *containerOfSelectedImage = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 0, 350)];

NSLog(@"%f", self.frame.size.width);
// image view of selected photo by user.
UIImageView *userImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nature_01.jpg"]];
[userImage setFrame :CGRectMake(0, 0, 340, 300)];

// comment button with action of recodering user's comment.
UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
//[commentButton setBackgroundColor : [UIColor clearColor]];
float y = userImage.frame.size.height + 5.0f;
[commentButton setFrame : CGRectMake(32.0f, y, 24.0f, 24.0f)];
[commentButton addTarget:self action:@selector(audioRecordAction) forControlEvents:UIControlEventTouchDown];
[commentButton setImage:[UIImage imageNamed:@"comments-24.png"] forState:UIControlStateNormal];


[containerOfSelectedImage addSubview:userImage];
[containerOfSelectedImage addSubview:commentButton];

[self addSubview:containerOfSelectedImage];
[self addSubView:self.commentContainerView];
return self;
4

2 回答 2

3

你需要增加框架containerOfSelectedImage。当前宽度为 0。

containerOfSelectedImage 的框架必须足够大,这样按钮才能放入其中。如果按钮在该框架之外,它会显示出来,但你不能触摸他。

要调试此问题,您可以使用Reveal App 之类的工具。如果您的任何按钮位于父级框架之外,则不会检测到触摸。

于 2013-11-04T18:33:06.327 回答
0

更改[commentButton addTarget:self action:@selector(audioRecordAction) forControlEvents:UIControlEventTouchDown];[commentButton addTarget:self action:@selector(audioRecordAction) forControlEvents:UIControlEventTouchUpInside];

于 2013-11-04T18:28:04.753 回答