2017-02-24 50 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. 您可以嘗試以編程方式設置導航欄。