我正在为我的应用程序使用快速操作并且它们正常工作,除了缺少导航栏(没有后退按钮)。这是我的代码:
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
let vc = storyboard.instantiateViewController(withIdentifier: "AddViewController") as! AddViewController
switch shortcutItem.type {
case "AddIncome":
vc.type = .income
app?.mainVC.dismiss(animated: false, completion: nil)
app?.mainVC.present(vc, animated: true, completion: nil)
case "AddExpense":
vc.type = .expense
app?.mainVC.dismiss(animated: false, completion: nil)
app?.mainVC.present(vc, animated: true, completion: nil)
default:
break;
}
}
mainVC
实际上是视图控制器,我在其中显示AddViewController
,即导航栏缺失的vc。
我似乎无法看出问题所在。我是否还需要做一些额外的工作才能使其正常工作?
答案 0 :(得分:1)
@Kobe当您展示任何视图控制器时,您需要添加导航控制器以显示导航栏。试试下面的代码。
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
let vc = storyboard.instantiateViewController(withIdentifier: "AddViewController") as! AddViewController
var nav = UINavigationController()
nav.viewControllers = [vc]
switch shortcutItem.type {
case "AddIncome":
vc.type = .income
app?.mainVC.dismiss(animated: false, completion: nil)
app?.mainVC.present(nav, animated: true, completion: nil)
case "AddExpense":
vc.type = .expense
app?.mainVC.dismiss(animated: false, completion: nil)
app?.mainVC.present(nav, animated: true, completion: nil)
default:
break;
}
}
现在您可以在AddViewController上添加后退按钮。
override func viewDidLoad() {
super.viewDidLoad()
let button = UIBarButtonItem(title: "Back", style:.plain, target: self, action: #selector(self.moveToPreviousScreen))
self.navigationItem.backBarButtonItem = button
}
希望它能帮到你。
答案 1 :(得分:0)
您可以考虑很多要点: