2010-12-09 90 views
1
觸發一個UIButton的IBAction爲

我新的目標C.我創建的方法來構建&顯示器內部的按鈕UIViewUIView是內部另一個命名內容查看視圖,添加內容查看作爲一個子視圖一個UIScrollView,那麼問題是,我不能生成的按鈕來觸發名爲playAction的IB行動。有人可以請幫我嗎?謝謝無法從UIView的

- (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"); 
} 
+0

是否按鈕轉藍色w當你點擊它? – Brian 2010-12-10 15:54:57

回答

1

使用UIControlEventTouchUpInside而不是UIControlEventTouchDown

+0

我試過你的建議,但仍然是同樣的問題。請有人幫忙,因爲我真的需要這個工作。 – Lilz 2010-12-10 07:35:08