【问题标题】:Show a viewController through another viewController, Swift通过另一个 viewController 显示一个 viewController,Swift
【发布时间】:2023-03-08 00:23:01
【问题描述】:

我的 UISearchController 的结果在一个单独的 UITableViewController (TableSearch) 中。

我希望,当我们选择一行时,显示带有 navBar 和 tabBar 的 DetailViewController,并有通向 SearchViewController 的后退按钮。

我试过了:

dispatch_async(dispatch_get_main_queue(), { () -> Void in

        var viewController = UIViewController()
        titreDetail = "Boo"
        viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("Detail") as! DetailViewController

       self.showViewController(viewController, sender: nil)
    })

但它呈现的 DetailViewController 没有 navBar 和 tabBar。

我试过这个之后:

dispatch_async(dispatch_get_main_queue(), { () -> Void in
        //SearchViewController
        var vc = UIViewController()
        vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("Search") as! SearchViewController
        //DetailViewController
        var viewController = UIViewController()
        titreDetail = "Boo"
        viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("Detail") as! DetailViewController

       vc.showViewController(viewController, sender: nil)
    })

什么都没有发生..

我怎样才能拥有类似的东西:

SearchTable -> SearchViewController -> DetailViewController ?

谢谢!

【问题讨论】:

  • 使用pushViewController 而不是presentViewController
  • 我试过 vc.navigationController?.pushViewController(viewController, animated: true) 和 self.navigationController?.pushViewController(viewController, animated: true)。什么都没有发生.. SearchTable 没有链接到 navigationController..
  • 你要么必须先弹出 searchTable,要么为新的 ViewController 再次创建 navBar/tabBar,或者也推送 searchTable 以继承 navBar/tabBar
  • 好了,tabBar显示在SearchTable上,UISearch outler在navBar中。而且我的 DetailViewController 没有嵌入到导航控制器中。即使我重新创建 tabBar 和 navBar,视图仍然会弹出。(我尝试使用他们的搜索视图获得与 Instagram 相同的结果!)

标签: swift segue viewcontroller pushviewcontroller presentviewcontroller


【解决方案1】:

使用 self.navigationController?.pushViewController(viewController, animated: true)

【讨论】:

  • 我试过 vc.navigationController?.pushViewController(viewController, animated: true) 和 self.navigationController?.pushViewController(viewController, animated: true)。什么都没有发生.. SearchTable 没有链接到 navigationController..
  • 你想从搜索视图控制器转到搜索表并且搜索表没有在顶部显示导航栏..对...?
  • 当我在 searchTable 上时,我想去 DetailViewController,它显示 DetailViewController 但在弹出时,navBar 和 Tabbar 消失了。..
【解决方案2】:

self.navigationController?.pushViewController(viewController, animated: true) 视图控制器是您创建的要推送的视图控制器的实例 - L.Valentine

【讨论】:

    【解决方案3】:

    实际上我创建了一个委托协议,它运行良好..

      protocol SearchTableDelegateProtocol {
      func didTapExtendInSearchViewControllerWithIndex(hashtag: String,  inTrending: SearchTable)
    }
    
    class SearchTable : UITableViewController, UISearchResultsUpdating {
    
    var delegate : SearchTableDelegateProtocol?
    ....
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("vc1")
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
    
            self.delegate?.didTapExtendInSearchViewControllerWithIndex(self.filtered[indexPath.row][0], inTrending: self)
    
        })
    }
    

    然后:

    class SearchViewController : UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, SearchTableDelegateProtocol {
    
    var resultSearchController = UISearchController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let locationSearchTable = storyboard!.instantiateViewControllerWithIdentifier("SearchTable") as!  SearchTable
        resultSearchController = UISearchController(searchResultsController: locationSearchTable)
        resultSearchController.searchResultsUpdater = locationSearchTable
    
        locationSearchTable.delegate = self
     }
    
    ...
    
    
    func didTapExtendInPhotoCollectionViewCellWithIndex(index: Int, inTrendingCell: CollectionCellOne) {
        print (index)
        self.performSegueWithIdentifier("hashtag", sender: "Instagram")
    
       }
     }
    

    谢谢大家!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-01
      • 2014-10-18
      • 2010-11-24
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多