iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何以编程方式重新排序我的UINavigationController项目?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_696_4@ 在我的TabBarViewController中,我正在创建一个导航并以模态方式呈现它.

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已经在堆栈中,请将其移动到堆栈的末尾,以便用户可以看到它.没有重复.我怎样才能做到这一点?
>否则,只需创建视图控制器的新实例并将其添加到堆栈顶部.我想我已经在上面这样做了.

重新排序后,所有“后退”按钮应按预期工作.

解决方法

您可以使用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({ $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,请注明来意。