我需要编写一些代码来在 iOS 应用程序启动时将 View 切换到另一个选项卡(因此,例如,默认显示第二个选项卡而不是第一个选项卡)。
我是 Swift 的新手,并且已经解决了以下问题:
代码应该放在第一个选项卡的 ViewController 的 override func viewDidLoad() 函数中。
以下代码显示了第二个 ViewController,但底部没有标签栏(vcOptions 是第二个 ViewController 标签项:
let vc : AnyObject! = self.storyboard.instantiateViewControllerWithIdentifier("vcOptions")
self.showViewController(vc as UIViewController, sender: vc)
我认为答案可能在于使用 UITabbarController.selectedIndex = 1,但不太确定如何实现它。
最佳答案
如果你的 window
rootViewController
是 UITabbarController
(在大多数情况下),那么你可以访问 tabbar
didFinishLaunchingWithOptions
在 AppDelegate
文件中。
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
if let tabBarController = self.window!.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 1
}
return true
}
这将打开带有 selectedIndex
中给定 (1) 的 index
的选项卡。
如果您在 firstViewController
的 viewDidLoad
中执行此操作,则需要通过标志或其他方式进行管理以跟踪所选选项卡。在 AppDelegate
文件的 didFinishLaunchingWithOptions
或 rootViewController
自定义类 viewDidLoad
中执行此操作的最佳位置。
关于ios - 以编程方式在 Swift 中的选项卡之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25325923/