【问题标题】:Show View Controller with Selected TableView Item and Tab Bar Controller显示具有选定 TableView 项和选项卡栏控制器的视图控制器
【发布时间】:2017-11-25 12:16:22
【问题描述】:

我正在尝试从我的Main.storyboard 文件中呈现一个SharkProfileTableViewController,并在没有标签栏的Upload.storyboard 的模态视图中的一个按钮操作中显示一个TabBarViewController

我试图展示的 ViewController 是来自 SharksTableViewController 表视图的选定项,该表视图基于模态视图中的数据。

如何将SharkProfileViewControllerTabBarViewController 一起显示?

@IBAction func viewProfileButtonPressed(_ sender: UIButton) {
    let stb = UIStoryboard(name: "Main", bundle: nil)
    let tabBar = stb.instantiateViewController(withIdentifier: "tabBar") as! TabBarViewController
    let sharkTabBar = stb.instantiateViewController(withIdentifier: "sharkTableView") as! SharksTableViewController
    let sharkProfile = stb.instantiateViewController(withIdentifier: "sharkProfile") as! SharkProfileTableViewController

    sharkProfile.selectedShark = shark as JSONObject

    tabBar.selectedIndex = 3

    self.present(tabBar, animated: true) {

    }

}

TabBarController - 'Shark' 选项卡是应该显示的选项卡

SharksTableViewController - 当在这个 tableview 中选择一个项目时,它会显示 ...

SharkProfileTableViewController - 这是我试图呈现的视图(显示标签栏)

【问题讨论】:

    标签: ios swift uitableview uitabbarcontroller uistoryboard


    【解决方案1】:

    如果您想将您在UITabbarController 中添加的任何数据传递给SharkProfileTableViewController,那么您可以使用UITabbarControllerviewControllers 属性访问它。 viewControllers 属性将返回 UIViewController 数组,因此您需要在 Tabbar 中使用带有控制器索引的下标来访问它。

    @IBAction func viewProfileButtonPressed(_ sender: UIButton) {
        let stb = UIStoryboard(name: "Main", bundle: nil)
        let tabBar = stb.instantiateViewController(withIdentifier: "tabBar") as! TabBarViewController
        //If SharkProfileTableViewController at 4 position in tabbar then access it from array like this
        let nav = tabBar.viewcontrollers?[3] as! UINavigationController 
        let sharkProfile = stb.instantiateViewController(withIdentifier: "sharkProfile") as! SharkProfileTableViewController
        sharkProfile.selectedShark = shark as JSONObject        
        tabBar.selectedIndex = 3        
        self.present(tabBar, animated: true) {
            nav.pushViewController(sharkProfile, animated: false)
        }
    }
    

    现在您只需更改viewcontrollers?[3] 中的索引即可从数组访问其他控制器。

    【讨论】:

    • 这会显示 Shark 选项卡并传递信息 - 但我想弄清楚的是如何进入下一个 ViewController。 SharksTableViewController 是选项卡,SharkProfileTableViewController 是在 SharksTableViewController 中选择特定鲨鱼时的下一个视图控制器。
    • 我添加了故事板的图像以帮助更好地展示这一点
    • @pmanning 不要只添加截图解释你想要的内容,还要提供一些关于截图的细节
    • @pmanning 呈现视图总是隐藏标签栏尝试推送视图,检查编辑的答案
    • @Nirav_D 很高兴知道!有效。甚至引入了导航控制器的后退按钮,这将是我的下一个任务。感谢所有的努力!我真的很感激时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 2014-07-30
    • 2019-01-08
    相关资源
    最近更新 更多