ios - 如何从 presentView Controller 返回到 RootViewController?

标签 ios swift

我正在开发一个 iPhone 照片图库应用程序,在我的应用程序中,我在图库中创建了一个文件夹,该图库文件夹显示在我的应用程序中,同时使用单击图像导航到下一个 View Controller ,他可以在其中看到完整的大小的图像,我在那里设置了垃圾按钮,这样用户就可以删除那些照片,我的垃圾按钮代码是:

@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)

                self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
                if(self.photosAsset.count == 0){

                    // No photos Left
                    self.imgView.image = nil
                    println("No Image Left")

                    // Pop to root view controller
                    self.navigationController?.popToRootViewControllerAnimated(true)

                }
                if(self.index >= self.photosAsset.count){
                    self.index = self.photosAsset.count - 1
                }
                self.displayPhoto()
        })

    }))
    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)
}

我已经设置,如果没有图像,则使用将使用这行代码导航到 RootViewController:

 self.navigationController?.popToRootViewControllerAnimated(true)

但这是行不通的,当最后一张图片被删除时,我得到了一个窗口:

enter image description here

我的控制台出现了:

2014-11-05 16:45:11.502 Photos Gallary App[3046:115145] Adding Image to Library -> Success
2014-11-05 16:45:15.285 Photos Gallary App[3046:115146] 
Deleted Image -> Success
No Image Left
2014-11-05 16:45:15.287 Photos Gallary App[3046:115146] *** Terminating app due to uncaught exception 'NSRangeException', reason: '0x7f95fb5ef980: index (18446744073709551615) beyond bounds (0)'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000100e4f3f5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010297dbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000100e4f32d +[NSException raise:format:] + 205
    3   Photos                              0x0000000100c5dd27 -[PHBatchFetchingArray objectAtIndex:] + 105
    4   Photos                              0x0000000100c63b92 -[PHFetchResult objectAtIndexedSubscript:] + 57
    5   Photos Gallary App                  0x0000000100b4d8d6 _TFC18Photos_Gallary_App9ViewPhoto12displayPhotofS0_FT_T_ + 694
    6   Photos Gallary App                  0x0000000100b50d93 _TFFFC18Photos_Gallary_App9ViewPhoto8btnTrashFS0_FPSs9AnyObject_T_U_FGSQCSo13UIAlertAction_T_U0_FTSbGSQCSo7NSError__T_ + 4243
    7   Photos Gallary App                  0x0000000100b4a6f6 _TTRXFo_dSboGSQCSo7NSError__dT__XFo_iTSbGSQS____iT__ + 38
    8   Photos Gallary App                  0x0000000100b4ed91 _TPA__TTRXFo_dSboGSQCSo7NSError__dT__XFo_iTSbGSQS____iT__ + 81
    9   Photos Gallary App                  0x0000000100b4a728 _TTRXFo_iTSbGSQCSo7NSError___iT__XFo_dSboGSQS___dT__ + 40
    10  Photos Gallary App                  0x0000000100b4a788 _TTRXFo_dSboGSQCSo7NSError__dT__XFdCb_dV10ObjectiveC8ObjCBooldGSQS___dT__ + 88
    11  libdispatch.dylib                   0x000000010541ccc6 _dispatch_call_block_and_release + 12
    12  libdispatch.dylib                   0x000000010543a7f4 _dispatch_client_callout + 8
    13  libdispatch.dylib                   0x0000000105425193 _dispatch_root_queue_drain + 1094
    14  libdispatch.dylib                   0x000000010542663c _dispatch_worker_thread2 + 52
    15  libsystem_pthread.dylib             0x00000001057d0ef8 _pthread_wqthread + 314
    16  libsystem_pthread.dylib             0x00000001057d3fb9 start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

谁能告诉我发生了什么以及解决方案是什么?

最佳答案

self.displayPhoto() 方法在做什么?你想在 pop 之后调用它吗?看起来很奇怪。

尝试在 self.navigationController?.popToRootViewControllerAnimated(true) 之后的行中添加一个“return”,以确保执行到此停止。

关于ios - 如何从 presentView Controller 返回到 RootViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26755916/

相关文章:

ios - 与 iPad 上的通知栏重叠

ios - 如何获得 UICollectionView 的滚动背景

ios - Storyboard 中的 UITableView 不会在旋转时更新内容大小

ios - swift : Get tableView indextPath/textLabel from selected UIView

ios - 如何在 iOS 中 GMSPolyline 的端点上绘制箭头?

ios - 类的解析列值始终为零

ios - 准备 segue 无法按预期工作

Swift 字典和可选交互

ios - swift : update of view constraints not visible

ios - 将数据传递给之前的 View Controller VIPER