Search code examples
iosswiftuisplitviewcontrollerviewcontrolleruipickerviewcontroller

how to show viewcontroller: UIImagePickController and dismiss it (Swift)


i got this uiactionsheet which loads uipickerviewcontroller its in a viewcontroller thats in a UISplitViewController

It's a UIsplitviewcontroller, this code is on a detailview, the detailview is called from masterview

how ever, when i try to load it "click it" it gives me a warning and doesnt go any further

   func actionSheet(actionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int){
    var imagePicker = UIImagePickerController()
    imagePicker.delegate = self

    switch buttonIndex{

    case 0:
        imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
        imagePicker.allowsEditing = true
        imagePicker.delegate = self
        NSLog("Vælg fra Biblioteket");
        break;
    case 1:
        imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
        imagePicker.allowsEditing = true
        imagePicker.delegate = self
        NSLog("Vælg Kamera");
        break;
    default:
        NSLog("Default");
        break;
    }
    self.presentViewController(imagePicker, animated: true, completion: nil) // this is the problem 
}

the warning is this : Warning: Attempt to present on which is already presenting (null)

how ever if i use this : self.showDeatilViewController(imagePicker, true) it show up but then i can't dismiss it at all

here's how i thought it would be dismissed

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {

    picker.dismissViewControllerAnimated(true, completion: nil)
}

If i run this code in viewDidLoad, it works

        var imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    imagePickerController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
    imagePickerController.allowsEditing = true
    self.presentViewController(imagePickerController, animated: true, completion: { imageP in

    })

I figured out that if i write this :

        self.presentedViewController?.presentViewController(imagePicker, animated: true, completion: nil)

it shows and closes ?!?!


Solution

  • I've got it working... someone brought to my attention that 8.0 IOS has a bug with opening the photolibery and such from apps.. so i put it in the main queue like this

    dispatch_async(dispatch_get_main_queue()){
            imagePicker.delegate = self
            self.presentViewController(imagePicker, animated: true, completion: nil)
        }
    

    and now it works!

    little delay but it works