2011-10-20 49 views
0

我创建了两个带有滚动页面的按钮。现在通过点击一个按钮应该可见其他按钮。在执行当前按钮的操作方法时,如何获得另一个按钮的索引?滚动页面时按钮事件无法正常工作

以下是创建按钮的代码:

 for (int i = 0 ; i < numberOfPages ; i++) 
{ 

    pageFrame = CGRectMake(i * scrollView.bounds.size.width, 0.0f, scrollView.bounds.size.width, 416) ; 


    btnSettingButton=[[UIButton alloc]initWithFrame:pageFrame ]; 
    [ btnSettingButton setFrame:CGRectMake(i * (scrollView.bounds.size.width)+50, 260, 20, 20)]; 
    btnSettingButton.tag=i; 
    [btnSettingButton setImage:[UIImage imageNamed:@"Settings Icon.png"] forState:UIControlStateNormal]; 

    [scrollView addSubview:btnSettingButton]; 
    [btnSettingButton setHidden:YES]; 
    // [btnSettingButton release]; 


    UIButton *btnCheckButton=[[UIButton alloc]initWithFrame:pageFrame ]; 
    [ btnCheckButton setFrame:CGRectMake(i * (scrollView.bounds.size.width)+250, 260, 20, 20)]; 
    [btnCheckButton setImage:[UIImage imageNamed:@"NO.png"] forState:UIControlStateNormal]; 


    [btnCheckButton addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside]; 

    // [btnCheckButton addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside]; 


    [scrollView addSubview:btnCheckButton]; 
    [btnCheckButton release]; 

回答

1

UIButtontag属性。你可以设置计数器标签,然后检查哪个按钮被点击。

btnCheckButton.tag = i; 

操作:

-(void)checkBoxClicked:(UIButton*)sender{ 
    if (sender.tag == 0){//button into the first page 
    } 
    if (sender.tag == 1){//second 
    } 
} 

为了找到您的视图的使用方法viewWithTag:

按钮