在选项卡栏中的特定选项卡中打开应用程序

问题描述 投票:0回答:6

我有一个带有 5 个选项卡的 TabBar 的应用程序(没有自定义类的常规选项卡栏)。当我启动应用程序时,左侧选项卡将打开。我想让它先打开中间的一个。我试过放

[self.tabBarController setSelectedIndex:3];

在首次打开的 ViewController 的 ViewDidLoad 中,但选项卡未切换。 我可以看到它突出显示但未选中。如果我将上面的代码放在

viewWillAppear
下,它将在第一次运行时被选中,但当我不时选择左侧选项卡时,它会跳到中间选项卡。

也尝试过但没有成功:

DetailsViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailsViewController"];
[self.navigationController pushViewController:vc animated:true];    

我做错了什么?

objective-c ios tabbar
6个回答
10
投票

这是唯一对我有用的解决方案。在应用程序委托中:

UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController; 
[tabBar setSelectedIndex:2];

1
投票

在故事板中将自定义类设置为 tabbarController 并在该自定义类中

-(void) viewDidLoad
{
    [self.tabBarController setSelectedIndex:3];
}

0
投票

有需要请致电

tabBarController.selectedIndex = desiredIndex;

应用程序内部已启动。


0
投票

尝试在 application:didFinishLaunchingWithOptions: 中设置它:如果你的选项卡控制器是 rootViewController

`[self.window.rootViewController.tabBarController setSelectedIndex:3];`

0
投票

我过去曾做过一些黑客行为。我发现为了使每个选项卡处于活动状态,在我的 applicationDidFinishLaunching 中,我需要循环遍历所有视图控制器,然后访问“view”属性:

for(viewController in arrayOfViewController)
{
    // will load the view controller into memory
    [viewController view];
}

不知道能不能用得上。


0
投票

我认为如果您使用标签栏控制器,您可以执行这些步骤。它对我的项目有效

-创建标签栏控制器

-创建新的 Cocotouch 类

-编写此代码:

import UIKit
import Foundation

class CustomTabBarController: UITabBarController {

@IBInspectable var initialIndex = 2

override func  viewDidLoad() {
    super.viewDidLoad()
  
selectedIndex = initialIndex

    }
}

我选择了第三个选项卡栏,您可以根据需要更改初始索引。

© www.soinside.com 2019 - 2024. All rights reserved.