大佬教程收集整理的这篇文章主要介绍了ios – 如何以编程方式重新排序我的UINavigationController项目?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
func viewDidLoad(){ super.viewDidLoad(); //Create a present this view controller in viewDidLoad self.navController = UINavigationController() self.presentViewController(self.navController,animated: true,completion: nil) } //This method gets called when TabBarVC gets a Nsnotification func showmessageForUser(user_id: int){ let mvc = self.storyboard?.instantiateViewControllerWithIdentifier("messagesViewController") as! messagesViewController mvc.user_id = user_id //set this user //display this to the user. self.navController.pushViewController(mvc,animated: truE) }
这很简单.但是,我想这样做:
>如果user_id已经在堆栈中,请将其移动到堆栈的末尾,以便用户可以看到它.没有重复.我怎样才能做到这一点?
>否则,只需创建视图控制器的新实例并将其添加到堆栈顶部.我想我已经在上面这样做了.
重新排序后,所有“后退”按钮应按预期工作.
这是我如何做到这一点.首先,我们向UIViewController添加一个方法,询问它是否是特定userId的视图控制器.由于大多数视图控制器不是(也可能不是)正确的视图控制器,因此它只返回false:
extension UIViewController { func isViewControllerForUserId(userId: int) -> Bool { return false } }
然后我们在messagesViewController中重写此方法,以在适当时返回true:
extension messagesViewController { override func isViewControllerForUserId(userId: int) -> Bool { return self.userId == userId } }
现在,为了显示特定用户的视图控制器,我们在导航控制器的堆栈中搜索现有的视图控制器.我们采取的行动取决于我们是否找到它:
func showmessageForUserId(userId: int) { if let index = navController.viewControllers.indexOf({ $0.isViewControllerForUserId(userId) }) { navController.moveToTopOfNavigationStack(viewControllerATindex: indeX) } else { pushNewViewControllerForUserId(userId) } }
private func pushNewViewControllerForUserId(userId: int) { let vc = self.storyboard?.instantiateViewControllerWithIdentifier("messagesViewController") as! messagesViewController vc.userId = userId self.navController.pushViewController(vc,animated: truE) }
如果我们找到它,我们使用此方法将其移动到导航堆栈的顶部:
extension UINavigationController { func moveToTopOfNavigationStack(viewControllerATindex index: int) { var stack = viewControllers if index == stack.count - 1 { // nothing to do because it's already on top return } let vc = stack.removeATindex(indeX) if (reorderingIsBuggy) { setViewControllers(stack,animated: falsE) } stack.append(vC) setViewControllers(stack,animated: truE) } private var reorderingIsBuggy: Bool { // As of iOS 9.3 beta 3,`UINavigationController` drops the prior top-of-stack // when you use `setViewControllers(_:animated:)` to move a lower item to the // top with animation. The workaround is to remove the lower item from the stack // without animation,then add it to the top of the stack with animation. This // makes it display a push animation instead of a pop animation and avoids // dropping the prior top-of-stack. return true } }
以上是大佬教程为你收集整理的ios – 如何以编程方式重新排序我的UINavigationController项目?全部内容,希望文章能够帮你解决ios – 如何以编程方式重新排序我的UINavigationController项目?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。