Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

using tag with actionsheet

basically i am trying to use tag with actionsheet because i am using two UIImages in the same view but each time i tried to save two differents images .Any Ideas Andrew Chalkley Amit Bijlani

2 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Can you post your code? I am not sure what are trying to do what is going wrong.

- (void)viewDidLoad
     {

      [super viewDidLoad];

      [self gesture1];
          [self gesture2];
   }
     -(void)gesture1

    {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ImagewasTApped:)];
    tap.cancelsTouchesInView = YES;
    tap.numberOfTapsRequired = 1;
    tap.delegate = self;

    [ _imageview addGestureRecognizer:tap];
    _imageview.userInteractionEnabled=YES;

}
-(void)gesture2{
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Image2wasTApped:)];
    tap2.cancelsTouchesInView = YES;
    tap2.numberOfTapsRequired = 1;
    tap2.delegate = self;

    [_imageview2 addGestureRecognizer:tap2];
    _imageview2.userInteractionEnabled=YES;

}

- (void) ImagewasTApped:(UIGestureRecognizer *)gestureRecognizer {
    UIActionSheet*sheet=[[UIActionSheet alloc]initWithTitle:@"Where did you save those pictures!!" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Library",@"camera", nil];
    [sheet showInView:self.view];

}
-(void)Image2wasTApped:(UIGestureRecognizer *)gestureRecognizer{
    UIActionSheet*sheet=[[UIActionSheet alloc]initWithTitle:@"Where did you save those pictures!!" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Library",@"camera", nil];
    [sheet showInView:self.view];

}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
        if(buttonIndex==0)
        {
            UIImagePickerController *imagepicker1=[[UIImagePickerController alloc]init];
            imagepicker1.delegate=self;
            imagepicker1.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
            imagepicker1.allowsEditing=NO;
            [self presentViewController:imagepicker1 animated:YES completion:NULL];

        }
        else if (buttonIndex==1)
        {
            UIImagePickerController *imagepicker2=[[UIImagePickerController alloc]init];
            imagepicker2.delegate=self;
            imagepicker2.sourceType=UIImagePickerControllerSourceTypeCamera;
            imagepicker2.allowsEditing=NO;
            [self presentViewController:imagepicker2 animated:YES completion:NULL];
        }

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   UIImage*orginalImage=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
    _imageview.image=orginalImage;

    UIImage*originalImage2=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
    _imageview2.image=originalImage2;
    [self dismissViewControllerAnimated:YES completion:NULL];
}


-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{

    [self dismissViewControllerAnimated:YES completion:NULL];
}


@end

Amit Bijlani Basically what i am trying to do is that : first when the app is running the user will double tap on both UIImages then save it to parse then load it in the app and if the person choose to change the picture the same process will keep repeatting . BUT I AM HAVING TROUBLE TO SAVE TWO DIFFERENTS PICTURES SO I WAS TRYING TO USE ENUMS

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

I don't think you need two tap gesture recognizers if you are assigning the final image to both UIImageViews. Although, I'm not sure if that solves your problem.

- (void)viewDidLoad
     {

      [super viewDidLoad];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
    tap.cancelsTouchesInView = YES;
    tap.numberOfTapsRequired = 1;
    tap.delegate = self;

    [ _imageview addGestureRecognizer:tap];
    _imageview.userInteractionEnabled=YES;

    [_imageview2 addGestureRecognizer:tap];
    _imageview2.userInteractionEnabled=YES;

}
- (void) imageTapped:(UIGestureRecognizer *)gestureRecognizer {
    UIActionSheet*sheet=[[UIActionSheet alloc]initWithTitle:@"Where did you save those pictures!!" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Library",@"camera", nil];
    [sheet showInView:self.view];

}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
            UIImagePickerController *imagepicker1=[[UIImagePickerController alloc]init];
            imagepicker1.delegate=self;
            imagepicker1.allowsEditing=NO;


      // since you are only changing the source type you need not create two instances 

        if(buttonIndex==0)
        {
            imagepicker1.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
        }
        else if (buttonIndex==1)
        {
            imagepicker1.sourceType=UIImagePickerControllerSourceTypeCamera;
        }
            [self presentViewController:imagepicker1 animated:YES completion:NULL];

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   UIImage*orginalImage=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
    _imageview.image=orginalImage;

    // you can assign the same original image to the other image view you need not create another one
    _imageview2.image=originalImage;
    [self dismissViewControllerAnimated:YES completion:NULL];
}


-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{

    [self dismissViewControllerAnimated:YES completion:NULL];
}

@end

Amit Bijlani i was trying to save two different images but with my previous code once i try to show a picture it kept showing the same picture on both UIImagePickerCOntroller