简体   繁体   English

在 Swift 中如何更改选中的标签栏

[英]In Swift How to change the selected tab bar

I am new to swift programming and I have a Tabbed application with 2 UITabBar items , I want that when user taping the second tab, application set the selected tab index to 0 and move user to the first tab.我是 swift 编程的新手,我有一个带有 2 个 UITabBar 项目的选项卡式应用程序,我希望当用户点击第二个选项卡时,应用程序将选定的选项卡索引设置为 0 并将用户移动到第一个选项卡。 I tried this code in my SecondViewController.swift's viewDidLoad :我在我的SecondViewController.swift's viewDidLoad尝试了这段代码:

 self.tabBarController?.selectedIndex = 0

but it doesn't work.但它不起作用。

This works:这有效:

override func viewDidAppear() {
    self.tabBarController?.selectedIndex = 0
}

See this answer for a good explanation of viewDidLoad() vs. viewDidAppear.有关 viewDidLoad() 与 viewDidAppear 的详细解释,请参阅此答案

Relevant excerpt:相关摘录:

This is where you want to perform any layout actions or do any drawing in the UI - for example, presenting a modal view controller这是您想要在 UI 中执行任何布局操作或进行任何绘图的地方 - 例如,呈现一个模态视图控制器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM