我正在为这个问题苦苦挣扎,当我切换与push segue连接的视图控制器时,一切都会按预期进行。问题是我有一个搜索栏,可以从代码中执行Table View Controller,并且当我从该Table View Controller中选择单元格时,下一个视图控制器没有选项卡栏。
我将Table View Controller用作显示搜索栏结果的视图。当我选择单元格(搜索结果)时,我正在更改显示结果的视图控制器。但这是从情节提要中完成的。 我知道从代码执行的Table View Controller不会通过导航栏“继承”以前的控制器的选项卡栏。但这应该从代码执行。
初始化搜索结果控制器
override func viewDidLoad() {
super.viewDidLoad()
self.definesPresentationContext = true
tableView.separatorStyle = .none
self.extendedLayoutIncludesOpaqueBars = true
refreshControlUI = Refresher.configureRefresher()
tableView.refreshControl = refreshControlUI
refreshControlUI.addTarget(self, action: #selector(pullToRefresh), for: .valueChanged)
// Initialize search table
let priceSearchTable = storyboard?.instantiateViewController(withIdentifier: "CoinSearchTable") as! CoinSearchTableViewController
// asignle pricetable as a search results controller
resultSearchController = UISearchController(searchResultsController: priceSearchTable)
resultSearchController?.searchResultsUpdater = priceSearchTable
// Make navigation bar large
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationItem.largeTitleDisplayMode = .never
self.navigationItem.searchController = resultSearchController
// customize search bar
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for coins"
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
}
该代码仅负责将值传递给选定的视图控制器。
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if shownAllCoins.count > 0 {
let coinString = shownAllCoins[indexPath.row]
picked = PickedCoin(symbols: [coinString])
picked.delegate = self
navigationController?.setNavigationBarHidden(false, animated: true)
picked.getDetail(name: coinString)
coin = picked.coins[0]
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToDetailsFromSearch" {
if let coin = sender as? [Any]{
if let SearchVC = segue.destination as? DetailPriceViewController{
SearchVC.coin = coin[0] as? Coin
SearchVC.isFromSearching = coin[1] as! Bool
}
}
}
}
在详细视图控制器中,我也必须以编程方式创建导航栏,以防segue来自搜索结果控制器。
func addNavBar() {
view.backgroundColor = UIColor(hex: 0x001f3e)
let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: view.frame.size.width, height: 44))
navBar.barTintColor = UIColor(hex: 0x001f3e)
navBar.isTranslucent = false
self.view.addSubview(navBar);
guard let coin = coin else {return}
let navItem = UINavigationItem(title: "\(coin.symbol)")
let backButton = UIButton(type: .custom)
let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
navBar.titleTextAttributes = textAttributes
saveButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.bookmarks, target: self, action: #selector(self.selectorName(_ :)));
backButton.setTitle("Back", for: .normal)
backButton.setTitleColor(backButton.tintColor, for: .normal) // You can change the TitleColor
backButton.addTarget(self, action: #selector(self.backAction(_:)), for: .touchUpInside)
navItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
navItem.rightBarButtonItem = saveButton
navBar.setItems([navItem], animated: false)
}
下面是故事板连接的屏幕截图。当然,这个没有导航栏的情节提要就是SearchResultController
(用于显示结果并切换到详细信息控制器的Table View Controller)。
将标签栏设置为根控制器或某些东西的最佳方法是什么。我只需要将标签栏放在所有视图中就可以了,无论控制器是从情节提要或代码初始化的,都没有关系。
我整天都在尝试解决此问题,但我不知道如何。 感谢您的帮助!
谢谢!
答案 0 :(得分:0)
好的,问题出在segues上。我试图从另一个控制器传递值,该值与以前的控制器没有什么共同之处,因此很明显导航栏和选项卡栏不存在。在处理单独的可服务视图控制器时,应添加委派,以将值传递回主控制器。与可搜索视图控制器交互时,在我的情况下,将值传递给另一个视图控制器的过程如下:
现在它按预期的方式工作。
简单的代码如下:
protocol SearchTableDelegate {
func passSelectedValue(selected stock: String)
}
在可搜索视图控制器中声明:
var delegate: SearchTableDelegate!
在DidSelectedRowAt中:
delegate.passSelectedValue(selected: selectedValue)
现在在主控制器中:
class MainTableViewController: UITableViewController, SearchTableDelegate {...}
并使用协议中的功能:
func passSelectedValue(selected value: String) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let destinationVC = storyboard.instantiateViewController(withIdentifier: "details") as? DetailsViewController{
destinationVC.value = value
self.navigationController?.pushViewController(destinationVC, animated: true)
}
}
此外,在MainController中声明SearchableViewController时,不要忘记将委托从Searchable分配给self:
searchableTableController.delegate = self