用参数调用UIAlertView委托方法?

时间:2012-11-23 07:49:42

标签: objective-c ios uibutton uialertview

我在表格视图的单元格上添加了一个按钮 -

UIButton* checkBoxBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[checkBoxBtn setBackgroundImage:[UIImage imageNamed:@"check_normal.png"] forState:UIControlStateNormal];
    [checkBoxBtn addTarget:self action:@selector(checkBoxAction:) forControlEvents:UIControlEventTouchUpInside];
    checkBoxBtn.tag = indexPath.row;
    [cell.contentView addSubview:checkBoxBtn];

这是我的checkBoxAction

-(void) checkBoxAction: (UIButton *)sender
{
    NSInteger i =sender.tag + 1;
    float perc = (i*100/18.0);
    NSLog(@"%.2f",perc);
    NSString* percentageStr = [[NSString alloc] initWithFormat:@"%3.2f%%(%d out of 18)",perc, i];
    barTitle.text = percentageStr;
    barFGImage.hidden = NO;
    if (perc == 100.00) {
        [barFGImage setFrame:CGRectMake(barFGImage.frame.origin.x, barFGImage.frame.origin.y, 276.0, barFGImage.frame.size.height)];
    }
    else
    [barFGImage setFrame:CGRectMake(barFGImage.frame.origin.x, barFGImage.frame.origin.y, 280*perc/100, barFGImage.frame.size.height)];
    [sender setBackgroundImage:[UIImage imageNamed:@"check_select.png"] forState:UIControlStateNormal];
    sender.userInteractionEnabled = NO;
}

现在一切都还可以,但我希望在用户按checkBoxBtn时显示提醒,如果用户按OK,则应调用checkBoxAction。我知道我们有UIAlertView代表方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

但问题是如何在其中获取checkBoxBtn?

编辑: Midhun MP的回答没问题,但是我又想要一个东西 - 在我的手机中我有三个标签和一个按钮like this screen shot,点击复选框我想要更改标签'6天内到期'以及我在checkBoxAction方法中完成的其他内容 请帮我。感谢。

1 个答案:

答案 0 :(得分:3)

为实现这一目标,您需要实施- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex委托方法。

你可以这样做:

在.h中声明一个UIButton实例 像:

UIButton *button;

更改按钮添加方法,如:

UIButton* checkBoxBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[checkBoxBtn setBackgroundImage:[UIImage imageNamed:@"check_normal.png"] forState:UIControlStateNormal];
[checkBoxBtn addTarget:self action:@selector(showAlert:) forControlEvents:UIControlEventTouchUpInside];
checkBoxBtn.tag = indexPath.row;
[cell.contentView addSubview:checkBoxBtn];


 - (void) showAlert:(id)sender
{
   button = (UIButton *)sender
   UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: @"Cancel"];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   if(buttonIndex == 0)
   {
      [self checkBoxAction:button];
   }
}

获取标签:

cellForRowAtIndexPath:中添加标签,如:

statusText.tag = 7;
[cell.contentView addSubview:statusText];

您可以使用以下代码获取标签:

UILabel *tempLabel = (UIlabel *)[[button superview] viewWithTag:7];
tempLabel.text = @"Midhun";