首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >隐藏ActionSheet iOS

隐藏ActionSheet iOS
EN

Stack Overflow用户
提问于 2014-01-21 11:20:57
回答 2查看 690关注 0票数 0

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

以下是代码:

代码语言:javascript
复制
- (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];

  }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-21 11:52:02

代码语言:javascript
复制
@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"];
 }
票数 0
EN

Stack Overflow用户

发布于 2014-01-21 11:29:54

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21256657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档