如何在推送的 ViewController (Xcode) 中获得后退按钮

Posted

技术标签:

【中文标题】如何在推送的 ViewController (Xcode) 中获得后退按钮【英文标题】:How do I get a back button in pushed ViewController (Xcode) 【发布时间】:2019-11-04 13:34:29 【问题描述】:

我有一个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
    

是否可以从以前的viewController 隐藏和禁用navigationItem 或将其更改为后退按钮?

这就是我推送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 没有嵌入到navigationController 中,如果有帮助的话。我尝试将其嵌入其中,但结果相同。

如果还有什么可以帮助的,请告诉我。

【问题讨论】:

【参考方案1】:

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];

【讨论】:

以上是关于如何在推送的 ViewController (Xcode) 中获得后退按钮的主要内容,如果未能解决你的问题,请参考以下文章

如何在没有 NavigationController 的情况下推送子 ViewController?

如何从 ViewController 中分离(推送)到 NavigationController 中嵌入的另一个 ViewController?

如何从 modalViewController 推送 ViewController

推送通知并检索其数据时如何在 ViewController 上执行操作

如何通过推送从 xib 转换到 ViewController

如何在作为导航控制器的插入视图的 tableview 中推送新的 viewController?