UITabBarController 并以编程方式切换到另一个选项卡
用户登录我的应用程序后,我会构建一些视图控制器和一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最终使用了静态方法并全局存储选项卡栏,以便稍后可以访问它。这是在名为“LoginViewController”的文件中声明的
然后在初始化我的导航控制器后,我像这样访问选项卡栏控制器并配置它:
然后我可以在任何地方访问它并以编程方式切换其视图:
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"
Then after initializing my navigation controllers, I access the tab bar controller like this and configure it:
Then I can access it anywhere and switch views on it programmatically:
任何具有父/祖先 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
.我假设你有一个 AppDelegate,对吗?如果是这样,您的代码如下:
}
然后,在您的逻辑中,使用
跨不同的控制器工作。在这里阅读详细信息:
视图控制器编程
I'm assuming you have an AppDelegate, correct? If so, you have code like this:
}
Then, in your logic, use
To work across the different controllers. Read details here:
View Controller Programming