如何在按下的 ViewController (Xcode) 中获得后退按钮
How do I get a back button in pushed ViewController (Xcode)
我有一个 viewController
,菜单按钮设置为 navigationItem
,如下所示:
self.navigationItem.setLeftBarButton(leftButton, animated: false)
。
它会调出一个抽屉式菜单,这在其上下文中很好。但是,如果我想从项目中使用后退按钮而不是菜单按钮更合适的其他地方推送 viewController
怎么办?也就是说,我只想回到之前的 viewController
而不是调出菜单。如何摆脱菜单按钮,取而代之的是在该特定实例中获得后退按钮?
这是 class 的代码。可以看到,这里设置了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
之前的 class 我尝试了类似的东西:
v.navigationItem.leftBarButtonItem = nil;
v.navigationItem.hidesBackButton = NO;
...什么也没做(是的,将它设置为 nil 是愚蠢的,只是想看看是否发生了什么事,但没有发生)。
我推送的 viewController
没有嵌入到导航控制器中,如果有帮助的话。我试过将它嵌入其中一个,但结果相同。
请告诉我是否还有其他有用的信息。
在 BaseViewController
中添加一个 Bool var:
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
}
}
}
然后,当你实例化控制器时,告诉它是否使用"menu"按钮:
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
,菜单按钮设置为 navigationItem
,如下所示:
self.navigationItem.setLeftBarButton(leftButton, animated: false)
。
它会调出一个抽屉式菜单,这在其上下文中很好。但是,如果我想从项目中使用后退按钮而不是菜单按钮更合适的其他地方推送 viewController
怎么办?也就是说,我只想回到之前的 viewController
而不是调出菜单。如何摆脱菜单按钮,取而代之的是在该特定实例中获得后退按钮?
这是 class 的代码。可以看到,这里设置了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
之前的 class 我尝试了类似的东西:
v.navigationItem.leftBarButtonItem = nil;
v.navigationItem.hidesBackButton = NO;
...什么也没做(是的,将它设置为 nil 是愚蠢的,只是想看看是否发生了什么事,但没有发生)。
我推送的 viewController
没有嵌入到导航控制器中,如果有帮助的话。我试过将它嵌入其中一个,但结果相同。
请告诉我是否还有其他有用的信息。
在 BaseViewController
中添加一个 Bool var:
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
}
}
}
然后,当你实例化控制器时,告诉它是否使用"menu"按钮:
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];