简体   繁体   English

如何“虚拟”点击导航栏中的后退按钮

[英]How to “virtually” tap back button in Navigation Bar

I have app, where is Segmented Control inside of my Navigation Bar. 我有应用程序,在导航栏内的“分段控制”中。 Under navigation bar I have 3 containers. 在导航栏下,我有3个容器。 In these containers I have Table View Controllers. 在这些容器中,我具有表视图控制器。 If you tap on segmented control, one TVC appear and others disappear (container1.hidden = true and so on). 如果您点击分段控件,则一个TVC出现,其他TVC消失(container1.hidden = true,依此类推)。

Problem is when I press "save" button which is also in navigation controller - button doesn't trigger "virtual push of back button". 问题是当我按下导航控制器中的“保存”按钮时-按钮不会触发“虚拟按下后退按钮”。

I used following code which works in my other projects (its in button's action which is in VC that contains all container views) but not this time: 我使用了以下代码在我的其他项目中起作用(在包含所有容器视图的VC中按钮的操作中起作用),但是这次不行:

if let navController = self.navigationController {
   navController.popViewControllerAnimated(true)
}

Image for better insight: 以获得更好洞察力的图像: 在此处输入图片说明

UPDATE : Thanks to @ Alexey Bondarchuk I solved it. 更新 :感谢@Alexey Bondarchuk,我解决了它。 Comments may be confusing so I just recap problem and solution. 评论可能会造成混淆,所以我只回顾问题和解决方案。

Originally, I had ViewController. 最初,我有ViewController。 To this controller I embed in Navigation Controller. 对于这个控制器,我嵌入了导航控制器。 To this Navigation Controller I connected segues. 我将segues连接到此导航控制器。 And that was mistake. 那是错误的。 So I deleted this embed in navigation controller, made (show) segues directly to my View Controller (which is on screenshot). 因此,我删除了此嵌入到导航控制器中的控件,将其直接显示到我的View Controller中(在屏幕截图中)。 This automatically created navigation bar and last thing I did was that I put navigation item in it so now my code pop right navigationController. 这会自动创建导航栏,最后我要做的是将导航项放入其中,现在我的代码弹出了navigationController。 Hope that it's understandable. 希望这是可以理解的。

I have couple ideas: 我有几个想法:

  1. Your navigationController equal to 'nil' and .popViewControllerAnimated will never invoked. 等于'nil'和.popViewControllerAnimated将永远不会被调用。 This may happen if you are using UITabBarController. 如果您使用的是UITabBarController,则可能会发生这种情况。 In this case try to use self.tabBarController?.navigationController instead of self.navigationController . 在这种情况下,请尝试使用self.tabBarController?.navigationController而不是self.navigationController

  2. Your controller presented 'Modally'. 您的控制器显示为“模态”。 In this case you can try to invoke navController.dismissViewControllerAnimated instead of navController.popViewControllerAnimated 在这种情况下,您可以尝试调用navController.dismissViewControllerAnimated而不是navController.popViewControllerAnimated

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM