无法从UIView触发UIButton的IBAction

时间:2010-12-09 16:39:46

标签: iphone objective-c uiview uiscrollview uibutton

我是Objective C的新手。我创建了构建&的方法。在UIView内显示一个按钮(UIView位于另一个名为contentView的View内,而contentView将作为UIScrollView中的子视图添加。问题是我无法点击生成的按钮触发名为playAction的IB动作。有人可以帮我吗?谢谢

- (void) displayCategoryBestSellers
{
    //Structure: ScrollView -> ContentView -> MainView -> ImageView and Button

    UIView * mainView = [[UIView alloc] initWithFrame:CGRectMake(0,0,160,105)];
    mainView.userInteractionEnabled = YES;

    UIImage * backgroundImage = [UIImage imageNamed:@"blackwhitesquare.png"];           
    UIImageView * uiImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,145,105)];
    uiImageView.image = backgroundImage;
    uiImageView.center = CGPointMake(mainView.frame.size.width /2, mainView.frame.size.height/2);   
    uiImageView.userInteractionEnabled = YES;

    [mainView addSubview:uiImageView];

    UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    playButton.frame = CGRectMake(0,0,100,90);

    NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://media.theseus.cataloguesolutions.com/images/72by72/324178611_0004_PG_1.jpg"]];
    UIImage *myimage = [[UIImage alloc] initWithData:mydata];

    [playButton setBackgroundImage:myimage forState:UIControlStateNormal];
    playButton.center = CGPointMake(mainView.frame.size.width /2, mainView.frame.size.height/2);        
    [playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchDown];     
    playButton.userInteractionEnabled = YES;

    [mainView addSubview:playButton];

    [contentView addSubview:mainView];

    contentView.userInteractionEnabled = YES;

    [scrollView addSubview:contentView];
    scrollView.delegate = self;

    scrollView.userInteractionEnabled = YES;        
}

-(IBAction)playAction: (id)sender
{
    NSLog(@"Button Clicked");
}

1 个答案:

答案 0 :(得分:1)

使用UIControlEventTouchUpInside代替UIControlEventTouchDown