Oli*_*cer 59 xcode uitabbarcontroller viewcontroller ios swift
当iOS应用程序启动时,我需要编写一些代码来将视图切换到另一个选项卡(例如,默认情况下显示第二个选项卡而不是第一个选项卡).
我是Swift的新手,并且已经解决了以下问题:
代码应该放在第一个选项卡的ViewController的override func viewDidLoad()函数中.
以下代码显示了第二个ViewController,但没有显示底部的选项卡栏(vcOptions是第二个ViewController选项卡项:
let vc : AnyObject! = self.storyboard.instantiateViewControllerWithIdentifier("vcOptions")
self.showViewController(vc as UIViewController, sender: vc)
Run Code Online (Sandbox Code Playgroud)
我认为答案可能在于使用UITabbarController.selectedIndex = 1,但不太确定如何实现它.
cod*_*ter 108
如果您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
}
Run Code Online (Sandbox Code Playgroud)
这将打开带有index给定(1)in 的选项卡selectedIndex.
如果你这样做在viewDidLoad你的firstViewController,你需要的标志或其他方法来跟踪所选标签的管理.在didFinishLaunchingWithOptions您的AppDelegate文件或rootViewController自定义类中执行此操作的最佳位置 viewDidLoad.
WTI*_*IFS 30
1.创建一个超级UITabBarController的新类.例如:
class xxx: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
Run Code Online (Sandbox Code Playgroud)
2.将以下代码添加到函数viewDidLoad():
self.selectedIndex = 1; //set the tab index you want to show here, start from 0
Run Code Online (Sandbox Code Playgroud)
3.转到故事板,将Tab Bar Controller的Custom Class设置为这个新类.(MyVotes1作为图中的例子)

Pon*_*boy 25
斯威夫特3
您可以将此代码添加到index 0tabBarController中的默认视图controller():
override func viewWillAppear(_ animated: Bool) {
_ = self.tabBarController?.selectedIndex = 1
}
Run Code Online (Sandbox Code Playgroud)
加载后,这会自动将选项卡移动到列表中的第二个项目,但也允许用户随时手动返回到该视图.
Ano*_*rak 18
要扩展@ codester的答案,您无需检查然后分配,您可以一步完成:
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
}
Run Code Online (Sandbox Code Playgroud)
Leo*_*aim 16
viewController必须是UITabBarControllerDelegate的子代.因此,您只需在SWIFT 3上添加以下代码即可
self.tabBarController?.selectedIndex = 1
Run Code Online (Sandbox Code Playgroud)
斯威夫特 5
//MARK:- if you are in UITabBarController
self.selectedIndex = 1
Run Code Online (Sandbox Code Playgroud)
或者
tabBarController?.selectedIndex = 1
Run Code Online (Sandbox Code Playgroud)
如果你使用 UITabBarController
// UITabBarDelegate
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("Selected item")
if kAppDelegate.isLoggedIn == false {
switch tabBar.selectedItem?.title {
case "Favorite":
DispatchQueue.main.async {
self.selectedIndex = 0
DispatchQueue.main.async {
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { timer in
self.showAlert(Constants.GlobalConstants.APP_NAME, message: "You are not login.")
}
}
}
break
case "Track Order":
DispatchQueue.main.async {
self.selectedIndex = 0
DispatchQueue.main.async {
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { timer in
self.showAlert(Constants.GlobalConstants.APP_NAME, message: "You are not login.")
}
}
}
break
default:
break
}
}
}
Run Code Online (Sandbox Code Playgroud)
在一个典型的应用程序中有一个 UITabBarController 并且它嵌入了 3 个或更多 UIViewController 作为它的选项卡。在这种情况下,如果您将 UITabBarController 子类化为 YourTabBarController ,那么您可以简单地通过以下方式设置所选索引:
selectedIndex = 1 // Displays 2nd tab. The index starts from 0.
Run Code Online (Sandbox Code Playgroud)
如果您从任何其他视图导航到 YourTabBarController,那么在该视图控制器的 prepare(for segue:) 方法中,您可以执行以下操作:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
if segue.identifier == "SegueToYourTabBarController" {
if let destVC = segue.destination as? YourTabBarController {
destVC.selectedIndex = 0
}
}
Run Code Online (Sandbox Code Playgroud)
我在 Xcode 10 和 Swift 4.2 中使用这种设置选项卡的方式。
| 归档时间: |
|
| 查看次数: |
86365 次 |
| 最近记录: |