我已经创建了两个滚动页面的按钮。现在,通过单击一个按钮,另一个按钮应该是可见的。当执行当前按钮的操作方法时,如何可能获得其他按钮的索引?
下面是创建按钮的代码: 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];
UIButton
具有tag
属性。您可以将计数器设置为标签,然后检查单击了哪个按钮。
btnCheckButton.tag = i;
行动:
-(void)checkBoxClicked:(UIButton*)sender{
if (sender.tag == 0){//button into the first page
}
if (sender.tag == 1){//second
}
}
为了找到按钮在您的视图使用方法viewWithTag: