2017-05-09 98 views
0

我有一個導航選項卡欄,具有不同的幾個選項卡。我想要一個功能,當我按下一個標籤時,無論我在哪裏,它都會使我看到視圖。選項卡導航欄一致性

例如:

「手冊」標籤欄 「打開文檔」另一個標籤欄

當導航到的手冊查看通過敲擊「手冊」,然後打開文件;我希望能看到開放數據模塊的列表,而不是跳到「最後打開的數據模塊」。

我該如何做到這一點?

的「打開文件」的代碼如下:

class DocumentListViewController: BaseViewController { 

// MARK: - IB Outlets 
@IBOutlet weak var tableView: UITableView! 

// MARK: - Properties 
var webview:WKWebView! 
var documentsToDelete:[Node]? 

// MARK: - VC Life Cycle 
override func viewDidLoad() { 

    super.viewDidLoad() 

    title = NSLocalizedString("Open Documents", comment: "") 

    tableView.delegate = self 
    tableView.dataSource = self 
    tableView.register(UINib(nibName: "TablistHeader", bundle: Bundle.main), forHeaderFooterViewReuseIdentifier: "Header") 

    webview = WKWebView(frame: .zero, configuration: WKWebViewConfiguration()) 
    webview.navigationDelegate = self 

} 

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    tableView.reloadData() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

//MARK: - Business Logic 
func remove(documents nodes:[Node]) { 

    documentsToDelete = nodes 

    if let section = nodes.first?.sections.first, let urlString = section["url"], let nodePath = nodes.first?.path { 

     var readAccessFolder:URL? 
     let pathComponents = URL(fileURLWithPath: nodePath).pathComponents 

     if pathComponents.count >= 2 { 

      let rootFolder = "\(pathComponents[1])" 
      readAccessFolder = URL(fileURLWithPath: "\(PackageManager.shared.packageFolder)\(rootFolder)") 
     } 

     let url = URL(fileURLWithPath: urlString) 
     _ = webview.loadFileURL(url, allowingReadAccessTo: readAccessFolder!) 

    } 
    else { 
     deleteDocumentsFromList() 
    } 

} 

fileprivate func deleteDocumentsFromList() { 

    if let indexes = self.documentsToDelete?.flatMap({ (node) -> Int? in 
     return PackageManager.shared.openNodes.index(of: node) 
    }) 
    { 

     for i in indexes.reversed() { 
      PackageManager.shared.openNodes.remove(at: i) 
     } 

     self.tableView.reloadData() 
     webview.load(URLRequest(url: URL(string:"about:blank")!)) 
    } 

} 

func resetProcedureStepCheckboxes() { 

    if let dmcs = documentsToDelete?.flatMap({ (node) -> String? in 
     return node.moduleCode 
    }) 
    { 
     webview.evaluateJavaScript("resetProcedureStepCheckboxes(\(dmcs))") { (result, error) in 
      print(error as Any) 
      self.deleteDocumentsFromList() 
     } 

    } 

} 

override func showDocumentViewController(for node:Node?, openSegment segment:Int = 0) { 

    let contentView = self.storyboard?.instantiateViewController(withIdentifier: "DocumentViewController") as! DocumentViewController 
    contentView.currentNode = node 
    contentView.initalSegment = segment 
    contentView.navigationItem.titleView = UILabel.forTitleView(withText: node?.title) 

    navigationController?.pushViewController(contentView, animated: true) 

} 

} 

// MARK: - UITableViewDataSource 
extension DocumentListViewController : UITableViewDataSource { 

func numberOfSections(in tableView: UITableView) -> Int { 

    if PackageManager.shared.openNodes.count > 0 { 
     tableView.removeEmptyMessage() 
     return 1 
    } else { 
     tableView.showEmptyMessage(message: NSLocalizedString("No open documents", comment: ""), viewController: self) 
     return 0 
    } 

} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return PackageManager.shared.openNodes.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let node = PackageManager.shared.openNodes[indexPath.row] 
    var dText = "" 

    if let man = node.manual { 
     dText = "\(man)\n" 
    } 

    if let mc = node.moduleCode { 
     dText = "\(dText)" + "\(mc)" 
    } 

    let cell = tableView.dequeueReusableCell(withIdentifier: "OpenDocumentCell", for: indexPath) 
    cell.imageView?.image = node.image() 
    cell.imageView?.contentMode = .scaleAspectFill 
    cell.editingAccessoryType = .none 
    cell.accessoryType = .disclosureIndicator 
    cell.textLabel?.text = node.title 

    cell.detailTextLabel?.numberOfLines = 0 
    cell.detailTextLabel?.text = dText 

    return cell 

} 

} 

// MARK: - UITableViewDelegate 
extension DocumentListViewController : UITableViewDelegate { 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return 60 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    tableView.deselectRow(at: indexPath, animated: true) 

    let node = PackageManager.shared.openNodes[indexPath.row] 
    showDocumentViewController(for: node) 

} 

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 

    if PackageManager.shared.openNodes.count == 0 { 
     return 0 
    } 

    return 50 
} 

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

    if PackageManager.shared.openNodes.count == 0 { 
     return nil 
    } 

    let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "Header") as! TablistHeader 
    header.titleLabel.text = String.localizedStringWithFormat(NSLocalizedString("%i open documents", comment: ""), PackageManager.shared.openNodes.count) 
    header.onDelete = { [weak self] in 

     guard let strongSelf = self else { return } 

     // show action sheet 
     let title = NSLocalizedString("Clear list", comment: "") 
     let msg = NSLocalizedString("Do you really want to clear the list of open documents?", comment: "") 
     let options = UIAlertController(title:title, message: msg, preferredStyle: .alert) 

     let deleteAll = UIAlertAction(title: NSLocalizedString("Clear list", comment: ""), 
             style:.destructive, 
             handler: { (action) -> Void in 

             strongSelf.remove(documents: PackageManager.shared.openNodes) 

     }) 
     options.addAction(deleteAll) 

     let cancel = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), 
            style:.cancel, 
            handler: nil) 
     options.addAction(cancel) 

     strongSelf.present(options, animated: true, completion: nil) 

    } 
    return header 

} 

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 

    let del = UITableViewRowAction(style: .destructive, title: NSLocalizedString("Delete", comment: ""), handler: { [weak self] (action, indexPath) -> Void in 

     guard let strongSelf = self else { return } 

     let node = PackageManager.shared.openNodes[indexPath.row] 
     strongSelf.remove(documents: [node]) 

    }) 

    return [del] 

} 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
} 

} 

// MARK: - WKNavigationDelegate 
extension DocumentListViewController : WKNavigationDelegate { 

func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { 
} 

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { 
    resetProcedureStepCheckboxes() 
} 

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { 
    print(error) 
} 

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { 

    if let abs = navigationAction.request.url?.absoluteString, abs == "about:blank" { 
     decisionHandler(.cancel) 
    } 

    decisionHandler(.allow) 
} 

} 
+0

你能提供屏幕截圖或代碼,表明你想要做什麼?我很難理解你的問題。 – toddg

+0

我用代碼更新了問題。這段代碼基本上顯示了在表格視圖中的手冊,當單擊一個單元格時,它將顯示該特定單元格的詳細頁面。現在,當我切換標籤欄,然後回到此標籤(打開文檔)時,它會顯示詳細的視圖,但我希望讓我看看桌面視圖。希望它現在更有意義,你可以幫助我。如果有的話,問,我會回覆。 – habed

回答

1

你可以設置你的UITabBarControllerDelegate,然後實現shouldSelectViewController

func tabBarController(_ tabBarController: UITabBarController, 
       shouldSelect viewController: UIViewController) -> Bool { 

    // check if tabBarController.selectedViewController is navController 
    // containing your detailViewController. If so: 

    navController.popViewController(animated: false) 
    return true 
} 

這可能是UX很差,如果它會彈出回你的tableview而它仍在觀察中。另一種選擇是等待轉換完成,然後在viewController不可見時彈出viewController。

+0

如何才能讓等待轉換完成後再將viewController彈出視圖之後? – habed

+0

如何設置UITabBarControllerDelegate?請幫忙。讚賞。 – habed

+0

我已UITabBarControllerDelegate這樣: 類DocumentListViewController:BaseViewController,UITabBarControllerDelegate { – habed

1

固定。我所要做的就是設置的UITabBarController在我RootTabBarViewController和設置在viewDidLoad中(「委託=自我」),並實現以下功能:

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) { 
if tabBarController.selectedIndex == 1 { 

    if let vc = viewController as? UINavigationController {   
    vc.popViewController(animated: false) 
    } 
} 
相關問題