将视图控制器更改为标签栏控制器

时间:2016-09-01 16:53:20

标签: ios swift uitabbarcontroller

我有一个基于标签的应用程序,带有2个标签(HomeViewController和SettingsViewController)。

在SettingsViewController上,我有一个按钮,将用户带到第三个视图(ChangeSomeSettingController)。

在第三个视图中,我有一个带有此功能的TableView

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    ...
    let settingsViewController = self.storyboard!.instantiateViewControllerWithIdentifier("SettingsViewController") as! SettingsViewController
    self.presentViewController(settingsViewController, animated:true, completion:nil)
}

这会将用户移回SettingsViewController。但是,我无法在屏幕底部显示标签栏,就像我第一次加载应用程序时一样。

如何返回到SettingsViewController并将标签保留在屏幕底部?

3 个答案:

答案 0 :(得分:2)

首先,presentViewController:animated:completion:将以模态方式呈现SettingsViewController。你需要的是一个UINavigationController流程。

1)添加创建UITabBarController enter image description here

2)选择具有UINavigationControler流量的项目(UIViewController)。

3)删除它

4)添加UITableViewController

5)将新的UITableViewController添加到UITabBarController enter image description here

6)选择UITableViewController并嵌入(编辑器>嵌入>导航控制器)到导航控制器。 enter image description here

7)添加UIViewController并在UITableViewControllerUIViewController

之间添加一个segue(显示)

8)结果最终结果应该是这样的: enter image description here

答案 1 :(得分:1)

在ChangeSomeSettingController中,如果你从SettingViewController pushViewController,你应该调用

navigationController?.popViewControllerAnimated(true)

或者如果你想回到SettingViewController

//back to root view controller from navigation
navigationController?.popToRootViewControllerAnimated(true)
// you should call to tabbar view controller 
UITabbarViewController *tabbarVC = (UITabbarViewController *)[UIApplication sharedApplication] delegate].window.rootViewController;
[tabBarController setSelectedIndex:1]; // 1 is index of SettingViewController

这是我的例子,根据您的问题,您可以修改以适应您的要求。希望对你有所帮助。

答案 2 :(得分:0)

这一行:

self.presentViewController(settingsViewController, animated:true, completion:nil)

不会将用户移回设置VC,它会显示设置VC的新实例,其中包含标签栏。

当您选择标签时,您需要推送VC,因此如果您想要返回,则不必再次展示它们。

如果您不想要导航控制器,则应显示标签栏控制器而不是设置vc。

相关问题