IOS開発コントロールビューday 13:TableViewのcellページのbuttonクリック応答弾窓はどのように実現しますか


周知のように、ViewControllerのbuttonのクリックイベントは次のとおりです.
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

このファイルの下で直接方法を実装します.
- (void):(UIButton *)btn
{
	UIAlertController *aler = [UIAlertController alertControllerWithTitle:@" " message:@" " preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *sureAler = [UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleCancel handler:nil];
	UIAlertAction *cancelAler = [UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleDestructive handler:nil];
	
    [aler addAction:sureAler];
    [aler addAction:cancelAler];
    [self presentViewController:aler animated:YES completion:nil];
}

ただし、Table Viewのcellではエラーが表示され、presentView Controlを呼び出すことはできません.cellページはUItableView Cellから継承され、設定時にどのボタンからのリクエストかを区別する必要があるため、tag値を設定する必要があります.
1、まず、このcellのヘッダファイルにカスタムエージェント(エージェント名AlertClickDelegate自作)を設定し、@interfaceの前に書く
@protocol AlertClickDelegate <NSObject>
-(void)clickTest:(NSInteger *)tag;
@end

そして@interfaceでエージェントを宣言
@property (nonatomic,weak)id<AlertClickDelegate>alertDelegate;

2、cellページのクリック方法で代理ジャンプを設定し、代理方法を宣言する(ここで代理方法はclickTest)
- (void)click:(UIButton *)btn
{
    if (self.alertDelegate != nil &&  [self.alertDelegate respondsToSelector:@selector(clickTest:)]) {
        [self.alertDelegate clickTest:btn.tag];
    }
}

3、TableView Controlでエージェント@interfaceを宣言する後にcellForRowメソッドで、このカスタムエージェントを宣言して使用するcellでエージェントcellを設定.alertDelegate = self; 4、TableView Controlで、本格的なクリックイベントの具体的な方法を書き込む
- (void)clickTest:(NSInteger *)tag
{
	if (tag == 0) {
		UIAlertController *aler = [UIAlertController alertControllerWithTitle:@" " message:@" " 								preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *sureAler = [UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleCancel handler:nil];
	UIAlertAction *cancelAler = [UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleDestructive handler:nil];
	
    [aler addAction:sureAler];
    [aler addAction:cancelAler];
    [self presentViewController:aler animated:YES completion:nil];
	}else if(tag == 1){
		UIAlertController *aler = [UIAlertController alertControllerWithTitle:@" " message:@" " 								preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *sureAler = [UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleCancel handler:nil];
	UIAlertAction *cancelAler = [UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleDestructive handler:nil];
	
    [aler addAction:sureAler];
    [aler addAction:cancelAler];
    [self presentViewController:aler animated:YES completion:nil];
	}
}