如何從presentView控制器返回到RootViewController?

[英]How to go back to RootViewController from presentView controller?


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 navigate to the next view controller where he can see a full size image and I set trash button there so user can delete that photos and my trashButton code is :

我正在開發一個iPhone照片庫應用程序,在我的應用程序中,我創建了一個文件夾到庫中,該庫文件夾顯示在我的應用程序中,當使用單擊圖像時,它導航到下一個視圖控制器,在那里他可以看到一個完整的圖像和我在那里設置垃圾按鈕,以便用戶可以刪除那些照片,我的trashButton代碼是:

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

I have set if there is not image left than use will navigate to the RootViewController with this line of code:

我設置了如果沒有剩下的圖像,則使用以下代碼行導航到RootViewController:

 self.navigationController?.popToRootViewControllerAnimated(true)

But this is not working and when the last image is deleted I got a window:

但這不起作用,當刪除最后一張圖片時,我有一個窗口:

enter image description here

and my console shows up:

我的控制台出現了:

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) 

Can anybody tell me what is happening and what is the solution for this?

任何人都可以告訴我發生了什么,解決方案是什么?

4 个解决方案

#1


12  

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

self.displayPhoto()的做法是什么?你想在流行后打電話嗎?這看起來很奇怪。

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

嘗試在self.navigationController?.popToRootViewControllerAnimated(true)之后的行添加“return”以確保執行在那里停止。

#2


3  

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

#3


1  

Have you tried with this line of code :

你嘗試過這行代碼嗎?

self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)

#4


0  

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

當你沒有剩下任何照片並執行popToRoot時,它會繼續執行以下行:

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.

嘗試對它們進行評論以查看問題是否存在,如果存在,則取消注釋以解決問題。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/11/05/7202e551214cc5721deb079941a5b361.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com