Objective c 是否使用参数调用UIAlertView委托方法?
我在表格视图的单元格中添加了一个按钮,如下所示-Objective c 是否使用参数调用UIAlertView委托方法?,objective-c,ios,uibutton,uialertview,Objective C,Ios,Uibutton,Uialertview,我在表格视图的单元格中添加了一个按钮,如下所示- UIButton* checkBoxBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [checkBoxBtn setBackgroundImage:[UIImage imageNamed:@"check_normal.png"] forState:UIControlStateNormal]; [checkBoxBtn addTarget:self action:@selector(c
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];
这是我的支票箱操作
-(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的回答没问题,但我还想做一件事——在我的单元格中,我有三个标签和一个按钮,通过单击复选框,我想更改标签“6天后到期”,以及我们在checkBoxAction
方法中完成的其他事情
请帮帮我。谢谢。要实现这一点,您需要实现
-(void)alertView:(UIAlertView*)alertView Clicked Button Index:(NSInteger)Button Index
委托方法
你可以这样做:
在.h中声明UIButton实例
比如:
更改按钮添加方法,如下所示:
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";
我在单元格上也有一个标签,我想像访问按钮一样访问它,所以必须发送另一个参数吗?您能解释一下吗?再次抱歉错误-在UIButton类型的对象上找不到属性视图更改循环:对于(UIView*查看[[[button superview]superview]子视图])我已编辑问题请检查,我无法访问我的标签yet@Blios:如果您有teamviewer,我可以提供帮助,或者我已经编辑了问题,请检查
UILabel *tempLabel = (UIlabel *)[[button superview] viewWithTag:7];
tempLabel.text = @"Midhun";