objective-c - 如何在推送的 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
}
}
是否可以隐藏和禁用navigationItem
上viewController
一个按钮或将其更改为后退按钮?
这就是我推动的方式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
我正在推送的内容没有嵌入到导航控制器中。我尝试将其嵌入其中,但结果相同。
让我知道是否还有其他有用的信息。
解决方案
在 中添加一个布尔变量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];
推荐阅读
- bash - bash source 一个结尾有退出命令的文件
- javascript - javascript中的DOT.files?这是什么意思?
- javascript - AWS Lambda & API Gateway - 来自浏览器的 500 但不是来自 curl
- ios - 关闭一个自定义弹出 UIViewController 并立即呈现另一个自定义弹出 UIViewController -SWIFT
- mongodb - 当我在终端中输入 mongod 时,我的 MongoDB 不工作
- angular - 部署失败时的角度 6 路由
- python - 在 Google VM (Ubuntu) 上安装 TA-lib
- python - 使用 pandas 的 csv 值总和
- wix - 错误 CNDL0027:File/@Name 属性的值不是有效的 8.3 兼容名称
- data-binding - Kotlin 2路绑定自定义视图