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 NSNotificationfunc 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已经在堆栈中,请将其移动到堆栈的末尾,以便用户可以看到它.没有重复.我怎样才能做到这一点?
>否则,只需创建视图控制器的新实例并将其添加到堆栈顶部.我想我已经在上面这样做了.
重新排序后,所有“后退”按钮应按预期工作.
解决方法 您可以使用setVIEwControllers(_:animated :)重新排列视图控制器堆栈.您无需执行任何特殊 *** 作即可使后退按钮正常工作.导航控制器根据其vIEwControllers数组中的第二项设置后退按钮(如果有第二项),并在更新vIEwControllers数组时更新后退按钮.这是我如何做到这一点.首先,我们向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({private func pushNewVIEwControllerForUserID(userID: Int) { let vc = self.storyboard?.instantiateVIEwControllerWithIDentifIEr("MessagesVIEwController") as! MessagesVIEwController vc.userID = userID self.navController.pushVIEwController(vc,animated: true) }.isVIEwControllerForUserID(userID) }) { navController.movetotopOfNavigationStack(vIEwControllerAtIndex: index) } else { pushNewVIEwControllerForUserID(userID) }}
如果我们没有找到它,我们制作一个新的视图控制器并推送它:
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项目?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)