How to go back to RootViewController from presentView controller?

后端 未结 4 1768
南方客
南方客 2021-01-14 06:15

I am developing a iPhone Photos Gallery app and In my app I created one folder into gallery and that gallery folder is shown up into my app and while use click on Image its

相关标签:
4条回答
  • 2021-01-14 06:45

    What is the method self.displayPhoto() doing? Do you want to call it after pop? It seems weird.

    Try adding a "return" at the line after self.navigationController?.popToRootViewControllerAnimated(true) to make sure that the execution stops there.

    0 讨论(0)
  • 2021-01-14 06:47

    When you don't have any photos left and do the popToRoot, it continues executing this lines:

    alert.addAction(UIAlertAction(title: "Cancle", style: .Cancel, handler: {(alertAction)in
    //Do not delete Photo
    
    self.dismissViewControllerAnimated(true, completion: nil)
    }))
    self.presentViewController(alert, animated: true, completion: nil)
    

    Try commenting them to see if the problem is there, and if it is, uncomment for solving it.

    0 讨论(0)
  • 2021-01-14 06:56

    I found solution for my question is :

     @IBAction func btnTrash(sender: AnyObject) {
    
        let alert = UIAlertController(title: "Delete Image", message: "Are You Sure To delete this Image?", preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: {(alertAction)in
            //Delete Photo
    
            PHPhotoLibrary.sharedPhotoLibrary().performChanges({
                let request = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection)
                request.removeAssets([self.photosAsset[self.index]])
                }, completionHandler: {(success, error )in
                    NSLog("\nDeleted Image -> %@", (success ? "Success" : "Error"))
    
                    alert.dismissViewControllerAnimated(true, completion: nil)
    
                    // here is the code for go back to root view controller
    
                    if(success){
                        // Move to the main thread to execute
                        dispatch_async(dispatch_get_main_queue(), {
                            self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
                            if(self.photosAsset.count == 0){
                                println("No Images Left!!")
                                self.navigationController?.popToRootViewControllerAnimated(true)
                            }else{
                                if(self.index >= self.photosAsset.count){
                                    self.index = self.photosAsset.count - 1
                                }
                                self.displayPhoto()
                            }
                        })
                    }else{
                        println("Error: \(error)")
                    }
            })
    
    
        }))
        alert.addAction(UIAlertAction(title: "Cancle", style: .Cancel, handler: {(alertAction)in
        //Do not delete Photo
    
        self.dismissViewControllerAnimated(true, completion: nil)
        }))
        self.presentViewController(alert, animated: true, completion: nil)
    }
    
    0 讨论(0)
  • 2021-01-14 07:08

    Have you tried with this line of code :

    self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
    
    0 讨论(0)
提交回复
热议问题