UITabBarController 并以编程方式切换到另一个选项卡

发布于 2024-11-05 05:19:46 字数 810 浏览 3 评论 0原文

用户登录我的应用程序后,我会构建一些视图控制器和一个 UITabBarController,然后它们会在我的应用程序的其余部分中持久存在。下面是代码:

    .......
//construction of view controllers, standard 

NSMutableArray *topLevelControllers = [[[NSMutableArray alloc] init] autorelease];
[topLevelControllers addObject: paymentNavController];
[topLevelControllers addObject: customerNavController];
[topLevelControllers addObject: historyNavController];    

UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];
tabBarController.delegate = self;
[tabBarController setViewControllers:topLevelControllers animated:NO];
tabBarController.selectedIndex = 1;

那么让我们说在我的 customerNavController 中我有一个表视图,我想将用户切换到 paymentNavController,同时也切换 tabBarController 的选定索引。

那么我如何从它包含的视图控制器之一访问该 UITabBarController 呢?

After a user logs in on my app, I then construct some view controllers and a UITabBarController that is then persistent through the rest of my app. Here is the code for that:

    .......
//construction of view controllers, standard 

NSMutableArray *topLevelControllers = [[[NSMutableArray alloc] init] autorelease];
[topLevelControllers addObject: paymentNavController];
[topLevelControllers addObject: customerNavController];
[topLevelControllers addObject: historyNavController];    

UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];
tabBarController.delegate = self;
[tabBarController setViewControllers:topLevelControllers animated:NO];
tabBarController.selectedIndex = 1;

So then lets say in my customerNavController I have a table view and I want to switch the user over to the paymentNavController, switching the selected index of the tabBarController as well.

So how can I, from one of the view controllers it contains, access that UITabBarController?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

郁金香雨 2024-11-12 05:19:46

我最终使用了静态方法并全局存储选项卡栏,以便稍后可以访问它。这是在名为“LoginViewController”的文件中声明的

static id gGlobalInstanceTabBar = nil;
+ (UITabBarController *) tabBarController
{
    if (!gGlobalInstanceTabBar)
    {
        gGlobalInstanceTabBar = [[UITabBarController alloc] init];
    }
    return gGlobalInstanceTabBar;
}

然后在初始化我的导航控制器后,我像这样访问选项卡栏控制器并配置它:

UITabBarController *tabBarController = [LoginViewController tabBarController];

然后我可以在任何地方访问它并以编程方式切换其视图:

    UITabBarController *tabBar = [LoginViewController tabBarController];
//do anything with view controllers, pass values etc here before switching views
[tabBar setSelectedIndex:1];

I ended up using a static method and storing the tab bar globally so I could access it later. This is declared in a file called "LoginViewController"

static id gGlobalInstanceTabBar = nil;
+ (UITabBarController *) tabBarController
{
    if (!gGlobalInstanceTabBar)
    {
        gGlobalInstanceTabBar = [[UITabBarController alloc] init];
    }
    return gGlobalInstanceTabBar;
}

Then after initializing my navigation controllers, I access the tab bar controller like this and configure it:

UITabBarController *tabBarController = [LoginViewController tabBarController];

Then I can access it anywhere and switch views on it programmatically:

    UITabBarController *tabBar = [LoginViewController tabBarController];
//do anything with view controllers, pass values etc here before switching views
[tabBar setSelectedIndex:1];
看透却不说透 2024-11-12 05:19:46

任何具有父/祖先 UITabBarController 的控制器(无论在层次结构中有多深)都可以通过 [self tabBarController] 访问它。

对于具有 navigationController 属性的 UINavigationController 也同样适用。

Any controller (however deep in the hierarchy it may be) that has a parent/ancestor UITabBarController can access it via [self tabBarController].

The same works for UINavigationController with the property navigationController.

追我者格杀勿论 2024-11-12 05:19:46

我假设你有一个 AppDelegate,对吗?如果是这样,您的代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];

return YES;

}

然后,在您的逻辑中,使用

[self.delegate ...]

跨不同的控制器工作。在这里阅读详细信息:
视图控制器编程

I'm assuming you have an AppDelegate, correct? If so, you have code like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];

return YES;

}

Then, in your logic, use

[self.delegate ...]

To work across the different controllers. Read details here:
View Controller Programming

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文