如何在也具有导航的 Tabbar 应用程序中调用 viewWillDisappear 方法

发布于 2024-12-02 00:43:48 字数 482 浏览 1 评论 0原文

我在我的应用程序中创建了 5 个选项卡。在 Tab1 中,我有 UITableView。在 didSelectRowAtIndexPath 上,我导航到另一个 UIView,其中显示了我的所有 5 个选项卡。我还在那个导航视图中播放歌曲。

现在,当我单击导航中的后退按钮并再次转到原始视图时,我可以调用viewWillDisappear(如预期和正常情况)。

但是,当我直接单击另一个选项卡时,导航视图中不会调用 viewWillDisappear 。为什么会发生这种情况?

我只是想,当我直接单击另一个选项卡时,Tab1 中的视图将调用viewWillDisappear。但导航视图不会调用该方法。

那么可能的解决方案是什么?请给一些提示...

I have created 5 Tabs in my application. In Tab1 i have UITableView. On didSelectRowAtIndexPath i am navigating to another UIView in which I am showing my all 5 Tabs. And I also play song in that navigated view.

Now when I click Back button in navigation and i again go to my original view, i am able to call viewWillDisappear (as expected and normal situation).

But when I click directly another tab then viewWillDisappear is not called in the navigated View. Why this Happens??

I have just thought in a way that when I directly clicks the another Tab then the view in Tab1 will call viewWillDisappear. But the navigated view will not call that method.

So what could be possible solutions?? kindly give some hints...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

幽蝶幻影 2024-12-09 00:43:48

让 viewWillDisappear 工作

self.definesPresentationContext = true

我通过调用viewDidLoad()

,否则, viewWillDisappear 不会被调用。这就是我的内容:

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    self.searchController.active = false
    tableView.reloadData()
}

希望这会有所帮助。

I got the viewWillDisappear to work by calling

self.definesPresentationContext = true

in viewDidLoad()

Otherwise, viewWillDisappear wasn't getting called. And this is what I have in it:

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    self.searchController.active = false
    tableView.reloadData()
}

Hope this helps.

拥抱影子 2024-12-09 00:43:48

我认为当您在选项卡之间切换时需要捕获该事件。当你从Tab1切换到Tab2时,如你所料,Tab1viewWillDisappear不会被调用。相反,Tab2viewWillAppear 将被调用。

否则,如果您想在切换选项卡时捕获事件,请检查 此链接

I think that you need to catch the event when you switch between tabs. When you switch from Tab1 to Tab2, as you expect, viewWillDisappear of Tab1 will not be called. Instead, the viewWillAppear of Tab2 will be called.

Else if you want to catch the event when you switch tabs, check this link.

喜爱纠缠 2024-12-09 00:43:48

这是因为您已经创建了 tabBarController 并且将其作为 mainView 中的 viewController 推送。

因此整个 TabBarController 被视为一个 viewController。

希望这对您有帮助。

That is because the you have created tabBarController and you are pushing it as a viewController from the mainView.

So whole TabBarController is treated as one viewController.

Hope this helps you.

眼眸里的快感 2024-12-09 00:43:48

如果要调用此方法,请创建 nsnotification center 对象 viewWillDisappear
当您想调用此方法时发布此通知。

if you want to call this method create the nsnotification center object viewWillDisappear
and when you want to call this method post this notification.

安穩 2024-12-09 00:43:48

这就是我让它发挥作用的方法。

// UITabBarControllerDelegate
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {

        print("Selected view controller")

        // do stuff...
        self.navigationController?.setNavigationBarHidden(false, animated: false)

        return true
    }

That's how I got it to work.

// UITabBarControllerDelegate
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {

        print("Selected view controller")

        // do stuff...
        self.navigationController?.setNavigationBarHidden(false, animated: false)

        return true
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文