2016-03-21 2 views
0

Я использую UIImagePickerView в своем коде, в котором есть три кнопки. Один для съемки, второй - для выбора фотографии и третий для отмены. Код, как указано ниже:Кнопка отмены не работает в UIImagePickerView

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select the operation to proceed?" 
                   delegate:self 
                 cancelButtonTitle:@"Cancel" 
                destructiveButtonTitle:nil 
                 otherButtonTitles:@"Take Photo", @"Select Photo", nil]; 
     [actionSheet showInView:self.view]; 

Когда я нажимаю кнопку «Отмена», она не работает.

Я использую метод UIImagePickerViewDelegate, как показано ниже.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    @try { 
     //set selected image in imageview by imagepickerview 

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; 
    imgProfilePic.image = chosenImage; 

    [picker dismissViewControllerAnimated:YES completion:nil]; 
    } 
    @catch (NSException *exception) { 
     NSLog(@"%@",exception.description); 
    } 
} 

//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
//{ 
// NSLog(@"@@@@"); 
// [picker dismissViewControllerAnimated:YES completion:NULL]; 
//} 

Пожалуйста, проверьте мой код UIImagePickerView и предоставить мне указания для правильного кода.

#pragma mark - Actionssheet delegate method for Image 

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    @try { 

    if(buttonIndex != 3) 
    { 

     UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.allowsEditing = YES; 

     if(buttonIndex == 0) 
     { 
      NSLog(@"Choose Photo from Camera"); 

      //simulator has no camera so app will crash, below call just provide the alert that device has no camera. 
      if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 

       UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" 
                     message:@"Device has no camera" 
                    delegate:nil 
                  cancelButtonTitle:@"OK" 
                  otherButtonTitles: nil]; 

       [myAlertView show]; 

      } 
      else 
      { 
       picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
      } 
     } 
     if(buttonIndex == 1) 
     { 
      NSLog(@"Choose Photo from Gallary"); 
      picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

     } 

//  if (buttonIndex == 3) 
//  { 
//   NSLog(@"###"); 
//  } 
//   
//  else 
//  { 
//   NSLog(@"Cancel the tab"); 
//   [picker dismissViewControllerAnimated:YES completion:NULL]; 
// 
//  } 
     [self presentViewController:picker animated:YES completion:nil]; 


    } 
    } 
    @catch (NSException *exception) { 
     NSLog(@"%@",exception.description); 

    } 
} 
+0

Вы работаете прошивка 9 или прошивка 8 – Chetan

+0

Отмены кнопки для чего? – user3182143

+0

При нажатии кнопки отмены, она закрывает вид действия листа – user3182143

ответ

1

Я думаю, что вы новичок в разработке приложений stackOverflow и iOS. То, что UIActionsheet и UIImagePickerController оба - разные вещи. Так что, если вы создать actiohsheet как:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select the operation to proceed?"delegate:self 
     cancelButtonTitle:@"Cancel" 
     destructiveButtonTitle:nil otherButtonTitles:@"Take Photo", @"Select Photo", nil]; 
     [actionSheet showInView:self.view]; 

Теперь вы можете назвать его метод кнопки, как следующее:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
    { 
     if (buttonIndex==0) 
      { 
        // take photo selected 
      } 
      else if (buttonIndex==1) 
      { 
        // select photo selected 
      } 
      else 
      { 
        // cancel button selected 
      } 
    } 

Вы можете также использовать UIAlertController, что является замена UIAlert и UIActionSheet из iOS8 так что вы можно использовать как следующие:

UIAlertController* AlertSheet = [UIAlertController alertControllerWithTitle:@"New ActionSheet" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Action" style:UIAlertActionStyleDefault // 
                 handler:^(UIAlertAction * action) { 
                  NSLog(@"Action"); 
                 }]; 
[AlertSheet addAction:defaultAction]; 

UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { 

}]; 
[AlertSheet addAction:cancleAction]; 
[self presentViewController:AlertSheet animated:YES completion:nil]; 
+0

Спасибо за ваш ответ. это полезно для меня. Еще раз спасибо. –

+0

добро пожаловать. :) –

0

Вы можете попробовать этот код свою работу для меня

UIAlertController* alert = [UIAlertController 
          alertControllerWithTitle:nil  // Must be "nil", otherwise a blank title area will appear above our two buttons 
          message:nil 
          preferredStyle:UIAlertControllerStyleActionSheet]; 

UIAlertAction* button0 = [UIAlertAction 
          actionWithTitle:@"Cancel" 
          style:UIAlertActionStyleCancel 
          handler:^(UIAlertAction * action) 
          { 
           // UIAlertController will automatically dismiss the view 
          }]; 

UIAlertAction* button1 = [UIAlertAction 
          actionWithTitle:@"Take photo" 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction * action) 
          { 
           // The user tapped on "Take a photo" 
           UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
           picker.delegate = self; 
           picker.allowsEditing = YES; 
           picker.sourceType = UIImagePickerControllerSourceTypeCamera; 

           [self presentViewController:picker animated:YES completion:NULL];        }]; 

UIAlertAction* button2 = [UIAlertAction 
          actionWithTitle:@"Choose Existing" 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction * action) 
          { 
           // The user tapped on "Choose existing" 
           UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
           picker.delegate = self; 
           picker.allowsEditing = YES; 
           picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

           [self presentViewController:picker animated:YES completion:NULL]; 
          }]; 

[alert addAction:button0]; 
[alert addAction:button1]; 
[alert addAction:button2]; 
[self presentViewController:alert animated:YES completion:nil]; 
0

UIActionSheet устарела, так как Iso 8. Вы должны использовать UIAlertController

UIAlertController* alert = [UIAlertController 
          alertControllerWithTitle:nil  // Must be "nil", otherwise a blank title area will appear above our two buttons 
          message:nil 
          preferredStyle:UIAlertControllerStyleActionSheet]; 

UIAlertAction* button0 = [UIAlertAction 
          actionWithTitle:@"Cancel" 
          style:UIAlertActionStyleCancel 
          handler:^(UIAlertAction * action) 
          { 
           // UIAlertController will automatically dismiss the view 
          }]; 

UIAlertAction* button1 = [UIAlertAction 
          actionWithTitle:@"Take photo" 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction * action) 
          { 
           // The user tapped on "Take a photo" 
           UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init]; 
           imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 
           imagePickerController.delegate = self; 
           [self presentViewController:imagePickerController animated:YES completion:^{}]; 
          }]; 

UIAlertAction* button2 = [UIAlertAction 
         actionWithTitle:@"Select Photo" 
         style:UIAlertActionStyleDefault 
         handler:^(UIAlertAction * action) 
         { 
          // The user tapped on "Select Photo" 
          UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init]; 
          imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
          imagePickerController.delegate = self; 
          [self presentViewController:imagePickerController animated:YES completion:^{}]; 
         }]; 

[alert addAction:button0]; 
[alert addAction:button1]; 
[alert addAction:button2]; 
[self presentViewController:alert animated:YES completion:nil]; 
Смежные вопросы