I need to execute an action (emptying an array), when the back button of a UINavigationController
is pressed, while the button still causes the previous ViewController
on the stack to appear. How could I accomplish this using swift?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
I accomplished this by calling/overriding
viewWillDisappear
and then accessing the stack of thenavigationController
like this:One option would be implementing your own custom back button. You would need to add the following code to your viewDidLoad method:
UPDATE:
Here is the version for Swift:
UPDATE 2:
Here is the version for Swift 3:
I was able to achieve this with the following :
Swift 3
Swift 4
No need of custom back button.
NO
override func willMove(toParentViewController parent: UIViewController?) { }
This will get called even if you are segueing to the view controller in which you are overriding this method. In which check if the "
parent
" isnil
of not is not a precise way to be sure of moving back to the correctUIViewController
. To determine exactly if theUINavigationController
is properly navigating back to theUIViewController
that presented this current one, you will need to conform to theUINavigationControllerDelegate
protocol.YES
note:
MyViewController
is just the name of whateverUIViewController
you want to detect going back from.1) At the top of your file add
UINavigationControllerDelegate
.2) Add a property to your class that will keep track of the
UIViewController
that you are segueing from.3) in
MyViewController
'sviewDidLoad
method assignself
as the delegate for yourUINavigationController
.3) Before you segue, assign the previous
UIViewController
as this property.4) And conform to one method in
MyViewController
of theUINavigationControllerDelegate
just do control + drag the bar item to below func. work like charm
Swift 3: