2014-01-21 144 views
0

我的應用程序是關於點擊一個名爲「uploadphoto1.png」的背景圖片的按鈕。當我點擊按鈕時,它會顯示一個ActionSheet讓我選擇用相機拍照或從圖書館拍照。 例如,當我從相機中選擇一張照片時,它會讓我選擇一張照片並在imageView中顯示照片,並將按鈕的背景圖像從upload.png更改爲save.png。 現在的問題是,當我點擊與背景圖像「save1.png」按鈕,它顯示我actionSheet。 我想隱藏actionSheet。隱藏ActionSheet iOS

下面是代碼:

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

    } 

回答

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

非常感謝,它的工作 – user3218885

0

有各種不同的方面,這能夠處理。一種方法是設置一個布爾標誌,當你不想點擊按鈕來調出actionSheet。然後,在- (IBAction)ButtonPicture:(UIButton *)sender中,您可以檢查布爾標誌來決定是否創建actionSheet並顯示它。

+0

非常感謝,它的工作 – user3218885