Charlie Charlie - 11 days ago 5
Swift Question

popping a view controller in different tab

UPDATE
This code appears to do what I want, but I am curious if this is a bad tactic now.

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
print("pop view controller from tab bar")

if tabBarController.selectedIndex == 2 {
print("appdel detected index 2 tab")
let navCont = viewController as! UINavigationController
navCont.popToRootViewControllerAnimated(true)
}
}


ORIGINAL QUESTION:
I have some code that will delete data another tab refers to. I want to make sure that when this code executes in [Edit Record VC] that it will force tab to pop back to the root view, even though the code executes on its own tab:

[ ---------------TAB BAR CONTROLLER ------------------ ]
TAB 0 TAB 1 TAB 2 TAB 3
| | | |
| | | |
[NAV CONTR 0] [NAV CONTR 1] [NAV CONTR 2] [NAV CONTR 3]
| |
| |
[Table VC] [Map VC]
\ /
\ /
\ /
[View Record VC]
|
|
[Edit Record VC]


In the above drawing, when I delete a record in [Edit Record VC], I want to pop Nav Controller 1 and Nav Controller 2 to the very first VC. How can I do this? I have tried every way I can think of and the code just isn't working.

The reason I want to do this is because if NAV CONTR 2 navigates to [Edit Record VC] and deletes the record, [View Record VC] will still be referencing that record, causing it to point to a deleted object. When I try to open the NAV CONTR 2 tab after deletion, it'll cause a crash

Answer

Multiple objects reacting to an event sounds like a job for NSNotification.

Make NAV CONTR 1 and NAV CONTR 2 instances of a navigation controller subclass that listens for a notification (resetToFirstController or something). Make posting that notification part of the delete logic.

When each controller receives the notification it pops to its root...or whichever "safe" controller you choose.