2017-02-24 48 views
0

我正在对我的应用使用快速操作,并且它们正常工作,除了导航栏丢失(无后退按钮)。这里是我的代码:使用快速操作时缺少导航栏

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

当您以模态方式呈现视图控制器时,不会出现后退按钮。 – dan

+0

@dan:你说得对。我以模态方式呈现它们。只需要获得导航控制器并进行推动。请提供这个答案,我会接受它。 – Kobe

回答

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 
} 

希望它能帮助你,谢谢。

0

有很多点,你可以考虑:

  1. 你有没有添加导航控制器。 (我知道它太基本但忘了一些)
  2. 你是否已将视图控制器设置为导航控制器的根?
  3. 您可以尝试以编程方式设置导航栏。