如何以编程方式在 UITabBarController 之间切换
我有一个带有 UITabBarController 的应用程序,其中第一个选项卡包含主页的 ViewController。我要做的就是在选项卡之间切换(这非常简单:[[self tabBarController] setSelectedIndex:index]),并从“HomePage”浏览 selectedTab 的出口。
只是为了解释一下自己: TabElement1--->TabElementX---->UISegmentedController:segmentY
问题是 UISegmentedController 为 nil,因为它尚未初始化(至少在我第一次执行该操作时)。我应该如何解决这个问题?选项卡元素装有笔尖。
编辑--这里有一些代码:
@implementation HomeViewController // Tab Indexed 0
// ...
- (void)playVideoPreview {
NSArray *array;
array = [[self tabBarController] viewControllers];
// This is a test where I programmatically select the tab AND the switch.
[[[array objectAtIndex:2] switches] setSelectedSegmentIndex:1];
[[self tabBarController] setViewControllers:array];
}
@end
@implementation TGWebViewController // Tab Indexed 2
// ...
@synthesize switches; // In .h file: @property (nonatomic, retain) IBOutlet UISegmentedControl switches; Properly linked within the XIB.
- (IBAction)switchHasChangedValue {
// Foo operations.
}
现在,我第一次触发 playVideoPreview 时,我设法进入索引为 2 的选项卡,TGWebViewController,但开关还不存在,所以我发现自己使用名为“switches”的分段控件,并选择了第一个分段。如果我回到 HomeViewController,然后我再次触发 playVideoPreview,我会得到正确的行为。
I have an application with UITabBarController, where the first tab contains a ViewController of an HomePage. What I have to do, is to switch between the tabs (this is pretty simple: [[self tabBarController] setSelectedIndex:index]), AND to navigate through outlets of the selectedTab from the "HomePage".
Just to explain myself: TabElement1--->TabElementX---->UISegmentedController:segmentY
The problem is that the UISegmentedController is nil because it is not initialized yet (at least the first time I do the operation). How should I fix this problem? The tab elements are loaded with nibs.
EDIT-- Here's some code:
@implementation HomeViewController // Tab Indexed 0
// ...
- (void)playVideoPreview {
NSArray *array;
array = [[self tabBarController] viewControllers];
// This is a test where I programmatically select the tab AND the switch.
[[[array objectAtIndex:2] switches] setSelectedSegmentIndex:1];
[[self tabBarController] setViewControllers:array];
}
@end
@implementation TGWebViewController // Tab Indexed 2
// ...
@synthesize switches; // In .h file: @property (nonatomic, retain) IBOutlet UISegmentedControl switches; Properly linked within the XIB.
- (IBAction)switchHasChangedValue {
// Foo operations.
}
Now the first time I fire playVideoPreview I manage to get Into the Tab Indexed 2, TGWebViewController, but switches doesn't exists yet, so I find myself with the segmentedControl named "switches" with the first segment selected. If I get back to HomeViewController, then I fire again playVideoPreview, I get the correct behaviour.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经使用代表和布尔值解决了这个问题。现在,当 TabBar 索引 2 处的 ViewController 加载完成时,它会向其委托发送一条消息,告知必须选择哪个段。
编辑这是代码(希望有帮助):
I've fixed the problem using the delegates and a boolean. Now, when the loading of the ViewController at index 2 of TabBar is finished, it sends a message to its delegate which tells what segment has to be selected.
EDIT Here's the code (hope it helps):