-1

守則A_UIViewController:A_UIViewController presentViewController B_UIViewController presentViewController的UIImagePickerController解僱A_UIViewController

PrestBViewController *aviewcontroller = [[PrestBViewController alloc] init]; 
[self presentViewController:aviewcontroller animated:YES completion:nil]; 

守則B_UIViewController:

-(void)presentAction { 
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 
    imagePickerController.delegate = (id)self; 
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 
    [self presentViewController:imagePickerController animated:YES completion:nil]; 
} 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    [picker.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil]; 
} 

當我運行的代碼,它崩潰的:

[picker.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil]; 

燦有人幫忙嗎?

+0

又是什麼崩潰說呢? – Joshua

+0

添加錯誤日誌 –

回答

0

守則A_viewcontroller

- (IBAction)PresentAction:(id)sender { 
    B_ViewController *aviewcontroller = [self.storyboard instantiateViewControllerWithIdentifier:@"aa"]; 
    [self presentViewController:aviewcontroller animated:YES completion:nil]; 
} 

守則B_viewController

@implementation ViewController{ 
    UIImagePickerController *picker; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; //Check if Camera Available 
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 

     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error" message:@"Device has no Camera" preferredStyle:UIAlertControllerStyleAlert]; 

     UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]; 
     [alertController addAction:ok]; 

     [self presentViewController:alertController animated:YES completion:nil]; 
    } 



- (IBAction)takePic:(id)sender { //Button to get image from camera 
    picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 
    picker.allowsEditing = YES; 
    picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    [self presentViewController:picker animated:YES completion:NULL]; 
} 

- (IBAction)chooosePic:(id)sender { //Button to get image from albums 
    UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 
    picker.allowsEditing = YES; 
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    [self presentViewController:picker animated:YES completion:NULL]; 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //Choose image 
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; 
    self.imageView.image = chosenImage; 
    [picker dismissViewControllerAnimated:YES completion:NULL]; 
} 
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { //if cancelled before selection 
    [picker dismissViewControllerAnimated:YES completion:NULL]; 
} 

工作正常,嘗試

相關問題