点击第一个按钮

时间:2016-07-21 11:57:53

标签: ios objective-c uibutton

我有5个按钮在行上点击第一个按钮只有第二个按钮应启用点击第二个按钮第三个按钮应该启用。直到第4个按钮。

任何人都可以建议我实施将不胜感激。

buttonArray = [[NSArray alloc] initWithObjects:@"4",@"3",@"4", nil];

//行索引路径的单元格添加了此代码。

for (int buttonCount = 0; buttonCount<  [[buttonArray objectAtIndex:indexPath.row] intValue]; buttonCount++)
{
    int buttonSpace = 10 * buttonCount + 10;
    cell.Button = [[CustomGoalButton alloc] initWithFrame:CGRectMake(buttonSpace + (buttonCount * 50),35, 50, 50)];
    cell.Button.layer.cornerRadius = 25;
    [cell.Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.Button.buttonIndex = buttonCount;
    cell.Button.tag =  100 * (indexPath.row + 1)+ buttonCount;
    if (buttonCount == 0) {
        cell.Button.userInteractionEnabled = YES;
    }
    else{
        cell.Button.userInteractionEnabled = NO;
    }

    [cell.ScrollView addSubview:cell.Button];
}


// Button Action
    -(void)buttonAction:(CustomButton *)sender{
         UITableViewCell *cell = ( UITableViewCell *)[self. UITableView dequeueReusableCellWithIdentifier:@"TableViewCell"];
        NSIndexPath *indexPath = [self. UITableView indexPathForCell:cell];
        if (sender.selectedRowIndex == indexPath.row) {
            sender.backgroundColor = [UIColor redColor];
            sender.userInteractionEnabled = NO;
            sender.tag = sender.tag+1;
        }
    }

4 个答案:

答案 0 :(得分:2)

我想你想要这种功能。我有示例代码,因此您将了解如何执行此操作 -

enter image description here

以下是示例代码 -

buttonArray = [[NSArray alloc] initWithObjects:@"4",@"3",@"4", nil];

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return buttonArray.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    for (int buttonCount = 0; buttonCount<  [[buttonArray objectAtIndex:indexPath.row] intValue]; buttonCount++)
    {
        int buttonSpace = 10 * buttonCount + 10;
        UIButton* Button = [[UIButton alloc] initWithFrame:CGRectMake(buttonSpace + (buttonCount * 50),35, 50, 50)];
        Button.backgroundColor=[UIColor redColor];
        Button.layer.cornerRadius = 25;
        [Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        Button.tag =  100 * (indexPath.row + 1)+ buttonCount;
        if (buttonCount == 0) {
            Button.userInteractionEnabled = YES;
            Button.backgroundColor=[UIColor greenColor];
        }
        else{
            Button.userInteractionEnabled = NO;
            Button.backgroundColor=[UIColor redColor];
        }

        [cell addSubview:Button];
    }

    return cell;

}
// Button Action
-(void)buttonAction:(UIButton *)sender{

    UITableViewCell *cell = ( UITableViewCell *)[sender superview];

    NSIndexPath *indexPath=[self.tableView indexPathForCell:cell];

    if ([cell viewWithTag:sender.tag+1]==nil) {

        UIButton *btn=(UIButton*)[cell viewWithTag:100 * (indexPath.row + 1)+ 0];

        btn.backgroundColor=[UIColor greenColor];

        btn.userInteractionEnabled=YES;

        sender.userInteractionEnabled=false;

        sender.backgroundColor=[UIColor redColor];

    }else{

    UIButton *btn=(UIButton*)[cell viewWithTag:sender.tag+1];

    btn.backgroundColor=[UIColor greenColor];

    btn.userInteractionEnabled=YES;

    sender.userInteractionEnabled=false;

    sender.backgroundColor=[UIColor redColor];

    }

}

注意:如果您不想要循环播放,请删除条件,例如 buttonAction 中的if ([cell viewWithTag:sender.tag+1]==nil),请仅使用其他部分。希望这会帮助你并解决你的问题。

答案 1 :(得分:1)

1)创建所有按钮的插座集合。禁用最后4个按钮。

2)创建一个oultet,使每个按钮都应该调用相同的方法。

下面的代码将修复您的要求。

@IBAction func allButtonsTapped(sender: AnyObject) {
    let index = numberButtons.indexOf(sender as! UIButton)
    if numberButtons.count > index+1 {
        let button = numberButtons[index!+1]
        button.enabled = true
    }

}

编辑:OBJC代码

-(void) allButtonsTapped:(id)sender {
    int index = [numberButtons indexOfObject:((UIButton *)sender)]
    if numberButtons.count > index+1 {
        UIButton *button = numberButtons.objectAtIndex(index+1)
        button.enabled = true
    }
}

答案 2 :(得分:0)

像这样标记你的按钮

button1.tag = 1
button2.tag = 2
button3.tag = 3
button4.tag = 4

将所有四个按钮的点击事件添加为“ buttonClicked ”。

我假设您在表格视图单元格中有四个按钮的属性。因此,在点击任何按钮时添加以下代码。

-(void) buttonClicked:(UIButton*)sender
{
CGPoint center= sender.center;
CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.tabelView];
NSIndexPath *indexPath = [self.tabelView indexPathForRowAtPoint:rootViewPoint];

UITableViewCell *cell = [_tabelView cellForRowAtIndexPath:indexPath];

switch (sender.tag) {
    case 1:
    {
        [cell.btn1 setEnabled:NO];
        [cell.btn2 setEnabled:YES];
        [cell.btn3 setEnabled:NO];
        [cell.btn4 setEnabled:NO];
    }
        break;
    case 2:
    {
        [cell.btn1 setEnabled:NO];
        [cell.btn2 setEnabled:NO];
        [cell.btn3 setEnabled:YES];
        [cell.btn4 setEnabled:NO];
    }
        break;
    case 3:
    {
        [cell.btn1 setEnabled:NO];
        [cell.btn2 setEnabled:NO];
        [cell.btn3 setEnabled:NO];
        [cell.btn4 setEnabled:YES];
    }
        break;
    default:
        break;
  }
} 

根据您的要求更改上述代码。希望它会对你有所帮助。

答案 3 :(得分:0)

你的问题的确切答案。我为你试了样。它运作完美。你可以使用相同的代码并获得解决方案。

ViewController.m

#import "ViewController.h"
#import "CustomCell.h"
@interface ViewController ()
{
   NSMutableArray *arrayTableData;
}
@end
@implementation ViewController
@synthesize tableviewCustomCellButton

- (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  arrayTableData = [[NSMutableArray alloc]initWithObjects:@"Asia",@"Europe",@"Atlantic",@"Africa",@"Antartica",nil];
}
- (void)didReceiveMemoryWarning
{
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return arrayTableData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];
   if(cell==nil)
   {
     NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
     cell = nibs[0];
   }
   cell.labelData.text = [arrayTableData objectAtIndex:indexPath.row];

   [cell.btnClick addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
   [cell.btnClick setTitle:[NSString stringWithFormat:@"%ld",(long)indexPath.row] forState:UIControlStateNormal];
   [cell.btnClick setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

   cell.btnClick.tag = indexPath.row;

   return cell;
}

-(void)buttonAction:(UIButton *)sender
{
  UIButton *btnNext = sender;
  int buttonTag = (int)sender.tag;
  NSLog(@"The buttonTag is - %d",buttonTag);
  buttonTag = buttonTag+1;
  NSIndexPath *indexPath;
  CustomCell *cellBut;
  if(buttonTag < arrayTableData.count)
  {
    for(int i=0;i<arrayTableData.count;i++)
    {
          if(buttonTag == i)
          {
            indexPath = [NSIndexPath indexPathForRow:buttonTag inSection:0];
            cellBut = (CustomCell *)[tableviewCustomCellButton cellForRowAtIndexPath:indexPath];
            btnNext = (UIButton*)[cellBut viewWithTag:buttonTag];
            btnNext.selected = YES;
          }
          else
          {
            indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            cellBut = (CustomCell *)[tableviewCustomCellButton cellForRowAtIndexPath:indexPath];
            btnNext = (UIButton*)[cellBut viewWithTag:i];
            btnNext.selected = NO;
          }
       }
  }
  else
  {
    for(int i=0;i<arrayTableData.count;i++)
    {
        if(buttonTag == 5)
        {
            buttonTag = buttonTag - (int)[arrayTableData count];
            indexPath = [NSIndexPath indexPathForRow:buttonTag inSection:0];
            cellBut = (CustomCell *)[tableviewCustomCellButton cellForRowAtIndexPath:indexPath];
            btnNext = (UIButton*)[cellBut viewWithTag:buttonTag];
            btnNext.selected = YES;
        }
        else
        {
            indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            cellBut = (CustomCell *)[tableviewCustomCellButton cellForRowAtIndexPath:indexPath];
            btnNext = (UIButton*)[cellBut viewWithTag:i];
            btnNext.selected = NO;
        }
     }
  }
}

在上面的代码中,我使用带有标签和按钮的自定义单元格。如果您单击1按钮,将选择第二个按钮,就像其他按钮的操作相同。检查我的答案。