首页 > 解决方案 > 如何在推送的 ViewController (Xcode) 中获得后退按钮

问题描述

我有viewController一个菜单按钮设置navigationItem如下:

self.navigationItem.setLeftBarButton(leftButton, animated: false).

它会弹出一个抽屉菜单,这在它的上下文中很好。但是,如果我想viewController从项目中的其他地方推送返回按钮而不是菜单按钮更合适怎么办?也就是说,我只想回到上一个viewController而不是调出菜单。如何摆脱菜单按钮,而在该特定实例中获得后退按钮?

这是该类的代码。如您所见,navigationItem这里设置了。

override func viewDidLoad() {
    super.viewDidLoad()

    self.setUserInterfaceStyleLight()
    loginStoryboard = UIStoryboard(name: "StoryboardOne", bundle: nil)
    let leftButton = UIBarButtonItem(image: UIImage(named: "menu-icon"), style: .plain, target: self, action:  #selector(self.leftSideMenuButtonPressed(_:)))
    self.navigationItem.setLeftBarButton(leftButton, animated: false)

    self.segmentController.tintColor = .white
    if showContacts == true {
        lastSegmentViewed = 1
    }

    Analytics.logEvent("contact_book", parameters: nil)

    NotificationCenter.default.addObserver(self, selector: #selector(userDidLogin(_:)), name: NSNotification.Name.UserDidLogin, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(applicationAndViewWasResumed(_:)), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
    self.view.backgroundColor = ThemTemplate.shared.getThem().secondaryColor

    if #available(iOS 13.0, *) {
        segmentController.backgroundColor = UIColor.gray
    } else {

        segmentController.backgroundColor = UIColor.clear
    }
}

是否可以隐藏和禁用navigationItemviewController一个按钮或将其更改为后退按钮?

这就是我推动的方式viewController

UIStoryboard *containerStoryboard = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
BaseViewController *v = [containerStoryboard instantiateViewControllerWithIdentifier:@"base"];
[self.navigationController pushViewController:v animated:YES];

在推新之前的课堂上,viewController我尝试了类似的方法:

v.navigationItem.leftBarButtonItem = nil;
v.navigationItem.hidesBackButton = NO;

...什么也没做(是的,将其设置为 nil 是愚蠢的,只是想看看是否发生了某些事情而没有发生)。

如果有帮助,viewController我正在推送的内容没有嵌入到导航控制器中。我尝试将其嵌入其中,但结果相同。

让我知道是否还有其他有用的信息。

标签: objective-cuiviewcontrolleruinavigationbaruinavigationitem

解决方案


在 中添加一个布尔变量BaseViewController

class BaseViewController: UIViewController {

    var bShowMenu: Bool = true

    override func viewDidLoad() {
        super.viewDidLoad()

        self.setUserInterfaceStyleLight()
        loginStoryboard = UIStoryboard(name: "StoryboardOne", bundle: nil)

        if bShowMenu {
            let leftButton = UIBarButtonItem(image: UIImage(named: "menu-icon"), style: .plain, target: self, action:  #selector(self.leftSideMenuButtonPressed(_:)))
            self.navigationItem.setLeftBarButton(leftButton, animated: false)
        }

        self.segmentController.tintColor = .white
        if showContacts == true {
            lastSegmentViewed = 1
        }

        Analytics.logEvent("contact_book", parameters: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(userDidLogin(_:)), name: NSNotification.Name.UserDidLogin, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(applicationAndViewWasResumed(_:)), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
        self.view.backgroundColor = ThemTemplate.shared.getThem().secondaryColor

        if #available(iOS 13.0, *) {
            segmentController.backgroundColor = UIColor.gray
        } else {

            segmentController.backgroundColor = UIColor.clear
        }
    }
}

然后,当您实例化控制器时,告诉它是否使用“菜单”按钮:

UIStoryboard *containerStoryboard = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
BaseViewController *v = [containerStoryboard instantiateViewControllerWithIdentifier:@"base"];

// if you do NOT want the menu button
v.bShowMenu = false

[self.navigationController pushViewController:v animated:YES];

推荐阅读