在UINavigation Back上重置viewcontroller(Reset viewcontroller on UINavigation Back)

编程入门 行业动态 更新时间:2024-10-23 12:29:28
UINavigation Back上重置viewcontroller(Reset viewcontroller on UINavigation Back)

TL; DR:

如何从自动放置的UINavigationController后退按钮完全重新创建/重置我正在导航回来的视图控制器?

OldVC < - CurrentVC

确保OldVC完全重置。

嗨,大家好,

我正在使用UINavigation和自动生成的后退按钮。 当用户按下后退按钮时,我希望导航堆栈中的前一个视图控制器完全重新实现 - 类似于创建视图控制器然后按下它的方式。

我尝试过以下但是无法找到解决方案:

on view将加载目标VC, self.view.setNeedsDisplay() - 不重新创建

手动从堆栈中删除旧的VC并在其中插入一个新的VC

解雇VC - 当主VC离开新VC时,调用self.dismissViewControllerAnimated(true, completion: nil)不会从堆栈中删除旧VC

我现在使用以下代码有一个临时解决方案,但是从UX的角度来看它是不对的,因为它依赖于自定义按钮和来自推送的不合逻辑的动画,使用户认为他们正在进一步进入应用程序而不是而不是出来,代码在下面并完全重新创建VC(从实例化中可以看出)。

private func goToStoryBoard(name: String) { let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewControllerWithIdentifier(name) self.navigationController?.pushViewController(vc, animated: true) }

UPDATE

看起来问题在于我创建自定义组件的方式 - 我将它们初始化为如下所示,但通过这样做,它们将不会更新:

class HomeMenuButton: UIButton { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.setTitleColor(InterfaceColors.MainFont, forState: .Normal) self.titleLabel?.font = InterfaceFonts.ButtonFont } }

TL;DR:

How can I fully recreate/reset a view controller that I'm navigating back to from the automatically placed UINavigationController back button?

OldVC <- CurrentVC

Ensure OldVC is completely reset.

Hi guys,

I'm using UINavigation and the automatically generated back button. When a user presses the back button, I want the previous view controller in the navigation stack to be fully reinstantiated - similar to how you would create the view controller then push to it.

I've tried the following but have been unable to find a solution:

on view will load of the target VC, self.view.setNeedsDisplay() - doesn't recreate

manually dropping the old VC from the stack and inserting a fresh one in it's place

dismissing the VC - when leaving the main VC to the new VC, calling self.dismissViewControllerAnimated(true, completion: nil) doesn't drop the old VC from the stack

I have a temporary solution in place now using the following code, however from a UX perspective it is not right, as it relies on a custom button and an illogical animation from the push that makes the user think they are going further into the app rather than coming out, the code is below and recreates the VC fully (as you can see from the instantiation).

private func goToStoryBoard(name: String) { let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewControllerWithIdentifier(name) self.navigationController?.pushViewController(vc, animated: true) }

UPDATE

It looks like the issue is with the way I am creating custom components - I initialise them once like below, but by doing it this way they will not update:

class HomeMenuButton: UIButton { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.setTitleColor(InterfaceColors.MainFont, forState: .Normal) self.titleLabel?.font = InterfaceFonts.ButtonFont } }

最满意答案

不要制作一个全新的视图控制器。 只需实现viewWillAppear:完全初始化视图。 当视图控制器的视图最初出现时以及出现时,由于弹出的视图控制器被弹出以显示视图控制器的视图,因此这两者都将起作用。

(如果以后出现任何情况,你不想在viewWillAppear:上重新初始化视图viewWillAppear: ,只需在那些场合引发一个Bool标志。)

Don't make a whole new view controller. Just implement viewWillAppear: to completely initialize the view. This will work the same both when the view controller's view initially appears and when it appears because the pushed view controller is being popped to reveal it.

(If any situations later arise where you don't want to reinitialize the view on viewWillAppear:, just raise a Bool flag on those occasions.)

更多推荐

本文发布于:2023-04-28 04:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330275.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UINavigation   viewcontroller   Reset

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!