向后导航至根导航控制器的按钮

时间:2019-09-27 05:17:19

标签: ios swift

我正在使用情节提要,并且有3个VC。当我使用VC3时,我希望后退按钮跳过VC2并转到VC1。

我尝试了以下方法,但在进入VC1之前VC2闪烁了

override func viewWillDisappear(_ animated: Bool) {
    self.navigationController?.popViewController(animated: true)
}

我的自定义后退代码:

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "arrow_back")
    self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "arrow_back")
    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: #selector(self.backPressed(_:)))
    self.navigationController?.navigationBar.barStyle = .black  
}

@objc func backPressed(_ sender: Any){
    print("here")
    self.navigationController?.popToRootViewController(animated: false)
}

3 个答案:

答案 0 :(得分:1)

viewDidLoad的更改

// check if the menu is collapsed or not

$('body').on('mousemove', function() {

    var scrollTimer;

    if ($(this).hasClass("sidebar-collapse")) {

        console.log('Check: Menu is collapsed');

        //Scrolling opens menu
        $(window).scroll(function() {
            // detect scrolling and add/remove classes
            var scroll = $(window).scrollTop();
            if (scroll) {
                $("body").addClass("sidebar-collapse");
            } else {
                $("body").removeClass("sidebar-collapse");
            }
            // set and start timer counting for 8 sec
            scrollTimer = setInterval(function() {
                //When time is up addClass back
                $("body").addClass("sidebar-collapse");
            }, 5000);
        });
    } else {

        console.log('Check: Menu Open (missing: .sidebar-collapse)');

        //Stay open if mouse is on menu
        $('.main-sidebar').on('mouseenter', function() {
            if (!$('body').hasClass("sidebar-collapse")) {
                console.log('ON MENU');
                // set and start timer counting for 5 sec
                clearInterval(scrollTimer);
            }
        });
    }
});

添加 如果您想初次使用控制器,请使用

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "arrow_back")
    self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "arrow_back")

     var backButton = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(backPressed(_:)))
     navigationController?.navigationBar.topItem?.backBarButtonItem = backButton
    self.navigationController?.navigationBar.barStyle = .black  
}

,如果不是导航控制器中的第一视图控制器

@objc func backPressed(_ sender: Any){
     self.navigationController?.popToRootViewController(animated: true) 
}

答案 1 :(得分:0)

如果您不希望VC2在VC1出现之前就闪过,并且如果VC1是根,则只需设置animated = false

从VC3覆盖操作,然后使用此命令:self.navigationController?.popToRootViewController(animated: false)

答案 2 :(得分:0)

在您的代码后退按钮中,首先自动执行popToViewController而不是viewWillDisappear方法调用,以便您在VC2上闪烁

在VC3中,添加返回BarButtonitem以执行将VC3移至VC1的操作

override func viewDidLoad {
        super.viewDidLoad()
        self.navigationItem.hidesBackButton = true
        let customBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItem.Style.plain, target: self, action: #selector(back))
        self.navigationItem.leftBarButtonItem = customBackButton
    }

这里退回UIBarButtonItem

的方法
@objc func back(sender: UIBarButtonItem) {
    let vc = VC1()
    self.navigationController.popToViewController(vc, animated: true) 
}