gpt4 book ai didi

objective-c - 使用参数调用 UIAlertView 委托(delegate)方法?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:12:50 25 4
gpt4 key购买 nike

我在表格 View 的单元格上添加了一个按钮,就像这样-

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 委托(delegate)方法

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

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

编辑: Midhun MP 的回答没问题,但我还想要一件事 - 在我的单元格中,我有三个标签和一个按钮 like this screen shot ,单击复选框我想更改标签“6 天内到期”以及我们在 checkBoxAction 方法中完成的其他操作请帮我。谢谢。

最佳答案

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

你可以这样做:

在.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";

关于objective-c - 使用参数调用 UIAlertView 委托(delegate)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13524945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com