You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在didFinishPickingMediaWithInfo中判断UIImagePickerViewController的触发按钮?

如何判断UIImagePickerViewController是由哪个按钮触发的?

这问题我做iOS项目时也碰到过,分享几个实用的实现方式,你可以根据场景选:


方法一:利用tag属性(最快捷)

这是最直接的方案,给每个按钮触发的UIImagePickerViewController设置不同的tag值,在代理方法里通过判断tag区分来源。

代码示例:

先在按钮点击事件里标记tag:

// Button1 点击事件
- (IBAction)button1Tapped:(UIButton *)sender {
    UIImagePickerViewController *picker = [[UIImagePickerViewController alloc] init];
    picker.delegate = self;
    picker.tag = 1; // 标记为button1触发
    [self presentViewController:picker animated:YES completion:nil];
}

// Button2 点击事件
- (IBAction)button2Tapped:(UIButton *)sender {
    UIImagePickerViewController *picker = [[UIImagePickerViewController alloc] init];
    picker.delegate = self;
    picker.tag = 2; // 标记为button2触发
    [self presentViewController:picker animated:YES completion:nil];
}

然后在代理方法里判断:

- (void)imagePickerController:(UIImagePickerViewController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    if (picker.tag == 1) {
        // 处理button1触发的选择结果
        NSLog(@"来自Button1的图片选择");
    } else if (picker.tag == 2) {
        // 处理button2触发的选择结果
        NSLog(@"来自Button2的图片选择");
    }
}

方法二:使用关联对象(更语义化)

如果觉得数字tag不够直观,怕后期混淆,可以用关联对象给picker绑定自定义标识(比如枚举、字符串),可读性更强。

代码示例:

先定义一个枚举(用字符串也可以):

typedef NS_ENUM(NSInteger, ImagePickerSource) {
    ImagePickerSourceButton1,
    ImagePickerSourceButton2
};

然后在按钮点击事件里设置关联对象:

#import <objc/runtime.h>

// 定义关联对象的唯一key
static const void *ImagePickerSourceKey = &ImagePickerSourceKey;

// Button1 点击事件
- (IBAction)button1Tapped:(UIButton *)sender {
    UIImagePickerViewController *picker = [[UIImagePickerViewController alloc] init];
    picker.delegate = self;
    // 关联自定义标识
    objc_setAssociatedObject(picker, ImagePickerSourceKey, @(ImagePickerSourceButton1), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self presentViewController:picker animated:YES completion:nil];
}

// Button2 点击事件
- (IBAction)button2Tapped:(UIButton *)sender {
    UIImagePickerViewController *picker = [[UIImagePickerViewController alloc] init];
    picker.delegate = self;
    objc_setAssociatedObject(picker, ImagePickerSourceKey, @(ImagePickerSourceButton2), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self presentViewController:picker animated:YES completion:nil];
}

代理方法里获取并判断:

- (void)imagePickerController:(UIImagePickerViewController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    ImagePickerSource source = [objc_getAssociatedObject(picker, ImagePickerSourceKey) integerValue];
    switch (source) {
        case ImagePickerSourceButton1:
            NSLog(@"来自Button1的图片选择");
            break;
        case ImagePickerSourceButton2:
            NSLog(@"来自Button2的图片选择");
            break;
        default:
            break;
    }
}

方法三:自定义UIImagePickerViewController子类(适合复杂场景)

如果项目里picker逻辑复杂,或者需要扩展更多属性,自定义子类添加标记属性会更清晰。

代码示例:

先创建子类:

@interface MyImagePickerViewController : UIImagePickerViewController
@property (nonatomic, assign) NSInteger triggerSource; // 用枚举或数字标记来源
@end

@implementation MyImagePickerViewController
@end

按钮点击时初始化子类并赋值:

// Button1 点击事件
- (IBAction)button1Tapped:(UIButton *)sender {
    MyImagePickerViewController *picker = [[MyImagePickerViewController alloc] init];
    picker.delegate = self;
    picker.triggerSource = 1;
    [self presentViewController:picker animated:YES completion:nil];
}

// Button2 点击事件
- (IBAction)button2Tapped:(UIButton *)sender {
    MyImagePickerViewController *picker = [[MyImagePickerViewController alloc] init];
    picker.delegate = self;
    picker.triggerSource = 2;
    [self presentViewController:picker animated:YES completion:nil];
}

代理方法里直接判断:

- (void)imagePickerController:(UIImagePickerViewController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    MyImagePickerViewController *myPicker = (MyImagePickerViewController *)picker;
    if (myPicker.triggerSource == 1) {
        NSLog(@"来自Button1的图片选择");
    } else if (myPicker.triggerSource == 2) {
        NSLog(@"来自Button2的图片选择");
    }
}

小提醒:

不管用哪种方法,记得给picker设置weak引用或者在dismiss后及时置空,避免循环引用哦~

内容的提问来源于stack exchange,提问作者Rezwan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:19:32