Search code examples
iosobjective-cuiactionsheet

Hide ActionSheet iOS


My application is about clicking on a button with background image named "uploadphoto1.png". When I click on the button, it shows me an ActionSheet letting me choose to take a picture with the camera or taking a picture from the library. When, for example, I choose a picture from the camera, it lets me select a picture and show the picture in an imageView and it change the background image of the button from upload.png to save.png. The problem now that when I click on the button with background Image "save1.png" it shows me the actionSheet. I want to hide the actionSheet.

Here is the code:

- (void)viewDidLoad
{
[super viewDidLoad];
 UIImage *buttonpicture =[UIImage imageNamed:@"uploadphoto1.png"];
[ButtonPicture setBackgroundImage:buttonpicture forState:UIControlStateNormal];
[ButtonPicture setFrame:CGRectMake(173, 269, 140, 30)];
 }

- (IBAction)ButtonPicture:(UIButton *)sender
{
NSLog(@"Button Picture Pressed");
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"Photo" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Picture From Camera",@"Picture From Gallery",nil];

[actionSheet setTag:1001];
[actionSheet showInView:self.view];

}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
    case 0:
    {
        // Take a picture from camera
        UIImagePickerController * picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;

        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentModalViewController:picker animated:YES];
    }
        break;
    case 1:
    {
        // take a picture from gallery photos
        UIImagePickerController * picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        [self.view addSubview:toolbar];
        [self presentModalViewController:picker animated:YES];

    }
        break;
    default:
    break;    }

   }

   - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[ButtonPicture setBackgroundImage:[UIImage imageNamed:@"save1.png"] forState:UIControlStateNormal];
[picker dismissModalViewControllerAnimated:YES];
imageview.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//[actionSheet showInView:Nil];

  }

Solution

  • @interface YourViewController
    {
        BOOL imageChanged;
    }
    
    
    
    - (void)viewDidLoad
    {
         [super viewDidLoad];
         imageChanged = NO;
         UIImage *buttonpicture =[UIImage imageNamed:@"uploadphoto1.png"];
         [ButtonPicture setBackgroundImage:buttonpicture forState:UIControlStateNormal];
         [ButtonPicture setFrame:CGRectMake(173, 269, 140, 30)];
    }
    
    - (IBAction)ButtonPicture:(UIButton *)sender
    {
         NSLog(@"Button Picture Pressed");
         UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"Photo"        delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Picture From Camera",@"Picture From Gallery",nil];
    
         [actionSheet setTag:1001];
         if (!imageChanged)
              [actionSheet showInView:self.view];
    
    }
    
    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        switch (buttonIndex)
        {
            case 0:   
            // Take a picture from camera
            UIImagePickerController * picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentModalViewController:picker animated:YES];
            break;
            case 1:
            // take a picture from gallery photos
            UIImagePickerController * picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            [self.view addSubview:toolbar];
            [self presentModalViewController:picker animated:YES];
            break;
            default:
            break;   
         }
    
     }
    
     - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
           [ButtonPicture setBackgroundImage:[UIImage imageNamed:@"save1.png"] forState:UIControlStateNormal];
           imageChanged = YES;
           [picker dismissModalViewControllerAnimated:YES];
           imageview.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
     }