简体   繁体   English

如何显示viewcontroller:UIImagePickController并将其关闭(快速)

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

i got this uiactionsheet which loads uipickerviewcontroller its in a viewcontroller thats in a UISplitViewController 我得到了这个uiactionsheet,它在UISplitViewController的viewcontroller中加载了uipickerviewcontroller

It's a UIsplitviewcontroller, this code is on a detailview, the detailview is called from masterview 这是一个UIsplitviewcontroller,此代码位于detailview上,从masterview调用detailview

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 我如何使用它:self.showDeatilViewController(imagePicker,true)它显示出来,但是我根本无法将其关闭

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 如果我在viewDidLoad中运行此代码,则可以工作

        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 ?!?! 它显示并关闭?!?!

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 我已经开始工作了……有人提醒我,8.0 IOS在打开应用程序的照片库等时出现了一个错误。所以我将其放在这样的主队列中

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 稍有延迟,但有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM