gpt4 book ai didi

ios - 将信息传递给 UIAlertView 委托(delegate)

转载 作者:行者123 更新时间:2023-12-01 17:52:18 25 4
gpt4 key购买 nike

在用户确认后,我正在尝试使用警报 View 删除 TableView 中的一行。但是我不知道如何让 UIAlertViewDelegate方法知道要删除表中的哪一行。

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
UIAlertView *alert_delete = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"Confirm Delete %@",[names objectAtIndex:indexPath.row] ] message:@"Warning all student data will be earsed" delegate:self cancelButtonTitle:@"Dismess" otherButtonTitles:@"YES", nil];
[alert_delete show];

// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}

在警报方法中,我尝试处理它以从表和数据库中删除行
    -(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString*title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:@"YES"]) {
// how to pass indexPath.row to alertview
[names removeObjectAtIndex:indexPath.row];
}


}

最佳答案

如果你想向委托(delegate)传递一些东西,那么在调用警报 View 之前向委托(delegate)类添加一个属性并将信息传递给它:

@interface AlertDelegate : NSObject <UIAlertViewDelegate>
@property (nonatomic) NSIndexPath *indexPath;
@end

// @implementation AlertDelegate omitted

并这样使用:
UIAlertView *alertView = ...;
AlertDelegate *delegate = [AlertDelegate new];
alertView.delegate = delegate;
delegate.indexPath = indexPathFromSomewhere;
[alertView show]; // or whatever

如果代表是 self ,那么这意味着将属性添加到 self (或使用私有(private)实例变量)。

在委托(delegate)方法中,您可以访问 indexPath:
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString*title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:@"YES"]) {
[names removeObjectAtIndex:self.indexPath.row];
}
}

关于ios - 将信息传递给 UIAlertView 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25911619/

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