我想以编程的方式添加一个视图和按钮,如下所示。
问题是该按钮不会响应点击事件。我的意思是它既没有高亮显示也不调用选择器。
原因是我想实现录音选项列表行,并带有播放按钮。该列表行应可选择以进行深入分析。因此,我得到了一个
如果有人有更好的方法来做到这一点,那么也可以是一种解决方案。
问题是该按钮不会响应点击事件。我的意思是它既没有高亮显示也不调用选择器。
原因是我想实现录音选项列表行,并带有播放按钮。该列表行应可选择以进行深入分析。因此,我得到了一个
RecordingView
,该子类化UIView
,它本身使用构造函数中的目标添加按钮。请参见下面的代码。如果有人有更好的方法来做到这一点,那么也可以是一种解决方案。
@implementation MyViewController
- (IBAction) myAction {
RecordingView *recordingView = [[RecordingView alloc] initWithFrame:CGRectMake(30, 400, 130, 50)withTarget:self];
[recordingView setUserInteractionEnabled:YES];
[[self view] addSubview:recordingView];
}
@implementation RecordingView
- (id)initWithFrame:(CGRect)frame withTarget:(id) target
{
self = [super initWithFrame:frame];
UIButton *playButton = [[UIButton alloc] initWithFrame:CGRectMake(185, 5, 80, 40)];
[playButton setTitle:@"Play" forState:UIControlStateNormal];
[playButton setTitleColor:[UIColor darkTextColor]forState:UIControlStateNormal];
// creating images here ...
[playButton setBackgroundImage:imGray forState: UIControlStateNormal];
[playButton setBackgroundImage:imRed forState: UIControlStateHighlighted];
[playButton setEnabled:YES];
[playButton setUserInteractionEnabled:YES];
[playButton addTarget: target
action: @selector(buttonClicked:)
forControlEvents: UIControlEventTouchDown];
[self addSubview:playButton];
return self;
}
当我以相同的方式向视图控制器.m文件中直接添加按钮时,按钮会对点击作出反应。 因此,RecordingView存在某些问题。这里我需要做些什么不同的事情吗?
此外,是否有更好的方法来提供触摸事件的目标和选择器?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法中使用这段代码。...不过前提是我得让它正常工作。^^ - JOG