根据激活UIImagePickerController

时间:2016-11-03 20:51:07

标签: ios objective-c uiimagepickercontroller

我有两个按钮,按下每个按钮时,会出现UIImagePickerController

我可以选择并检索照片。但是,我想根据哪个按钮启动操作来做一些不同的事情。

有没有办法确定哪个按钮导致初始UIImagePickerController出现在didFinishPickingImage来电?

2 个答案:

答案 0 :(得分:2)

没有魔力。您需要以某种方式保存您的状态,并在调用委托方法时,确定您的状态并采取相应的行动。最简单的方法是向控制器添加属性或实例变量,并使用每个按钮的唯一值(例如枚举)对其进行更新。

答案 1 :(得分:0)

您可以为两个按钮分配标记属性,然后在创建UIImagePickerController实例时,可以为触摸到UIImagePickerController实例的picker.view.tag属性的按钮分配按钮标记属性。

然后在UIImagePickerController的委托回调中,您可以检查选择器的view.tag属性,以确定在创建uiimagepickercontroller时触摸了哪个按钮

mybutton.tag = 100;
[myButton addTarget:self action:@selector(displayTheUIImagePickerController:)     forControlEvents:UIControlEventTouchUpInside];



-(void)displayTheUIImagePickerController:(id)sender
{
    NSInteger buttonTag = [sender tag];

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.view.tag = buttonTag;

//Finish setting up picker controller and then present
}



- (void)imagePickerController: (UIImagePickerController *)picker
    didFinishPickingImage: (UIImage *)i
              editingInfo: (NSDictionary *)editingInfo {

    NSInteger buttonTag = picker.view.tag;


    switch (buttonTag) {
        case 100:
            //Do something for button with tag 100
            break;

        case 101:
            //Do something for button with tag 101
            break;

        default:
            break;
    }

}