隐藏ActionSheet iOS

时间:2014-01-21 11:20:57

标签: ios objective-c uiactionsheet

我的应用程序是关于点击一个名为“uploadphoto1.png”的背景图片的按钮。当我点击按钮时,它会显示一个ActionSheet,让我选择用相机拍照或从图书馆拍照。 例如,当我从相机中选择一张图片时,它让我选择一张图片并在imageView中显示图片,并将按钮的背景图像从upload.png更改为save.png。 问题是,当我点击带有背景图片“save1.png”的按钮时,它会显示actionSheet。 我想隐藏actionSheet。

以下是代码:

- (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];

  }

2 个答案:

答案 0 :(得分:0)

有多种方法可以处理。一种方法是在您不想点击按钮以调出actionSheet时设置布尔标志。然后,在- (IBAction)ButtonPicture:(UIButton *)sender中,您可以检查布尔标志,以决定是否创建actionSheet并显示它。

答案 1 :(得分:0)

@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"];
 }