我的应用程序是关于单击一个名为“upadpho1.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];
}发布于 2014-01-21 11:52:02
@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"];
}发布于 2014-01-21 11:29:54
处理这个问题的方法有很多种。一种方法是在不想点击按钮以打开actionSheet时设置布尔标志。然后,在- (IBAction)ButtonPicture:(UIButton *)sender中,您可以检查布尔标志以决定是否创建actionSheet并显示它。
https://stackoverflow.com/questions/21256657
复制相似问题