Ios4 如何以编程方式在UITabBarController之间切换

Ios4 如何以编程方式在UITabBarController之间切换,ios4,uitabbarcontroller,uisegmentedcontrol,Ios4,Uitabbarcontroller,Uisegmentedcontrol,我有一个使用UITabBarController的应用程序,其中第一个选项卡包含主页的ViewController。我要做的是在选项卡之间切换(这很简单:[[self-tabBarController]setSelectedIndex:index]),并从“主页”浏览所选选项卡的出口 我来解释一下:TabElement1-->TabElementX-->UISegmentedController:segmentY 问题是UISegmentedController为nil,因为它尚未初始化(至少在

我有一个使用UITabBarController的应用程序,其中第一个选项卡包含主页的ViewController。我要做的是在选项卡之间切换(这很简单:[[self-tabBarController]setSelectedIndex:index]),并从“主页”浏览所选选项卡的出口

我来解释一下:TabElement1-->TabElementX-->UISegmentedController:segmentY

问题是UISegmentedController为nil,因为它尚未初始化(至少在我第一次执行此操作时)。我应该如何解决这个问题?选项卡元素加载了NIB

编辑--下面是一些代码:

@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,但开关还不存在,因此我发现自己使用了名为“开关”的分段控件,并选择了第一个分段。如果我回到HomeViewController,然后再次启动playVideoPreview,我将获得正确的行为。

我已使用代理和布尔值修复了问题。现在,在TabBar的索引2处加载ViewController完成后,它会向其代理发送一条消息,告知必须选择哪个段

编辑以下代码(希望有帮助):

//第一个视图中要求对选项卡栏进行制表以启动另一个选项卡的方法
//视图控制器
-(无效)播放视频预览{
NSArray*阵列;
数组=[[self-tabBarController]视图控制器];
if(![[array objectAtIndex:2]catSwitch]){
[[array objectAtIndex:2]setDelegate:self];
[[array objectAtIndex:2]sethasbeenlaunchedbyelegate:YES];
}否则{
[自选标签];
}
[[self-tabBarController]SetViewController:array];
[[self-tabBarController]设置所选索引:2];
}
//现在,第二个视图控制器执行的操作
-(无效)代码中的某个地方{
如果(Hasbeenlaunchedbyelegate){
[[self delegate]selectTab];
}
}
//在第一个视图控制器中,这是委托方法,
//从第二个视图控制器启动
-(无效)选择选项卡{
NSArray*阵列;
数组=[[self-tabBarController]视图控制器];
[[[array objectAtIndex:2]catSwitch]setSelectedSegmentIndex:[[bannerPreview pageControl]currentPage]];
}
//一些声明
@协议二视图控制器;
类SecondViewController:ViewController{
id代表;
}
@结束
@协议二级ViewControllerDelegate
-(作废)选择Tab;
@结束
//同时在第一种观点中
类FirstViewController:ViewController{
// ...
}

当UISegmentedCOntrol为零时,该代码在哪里?在viewDidLoad中,VIEW将出现或在应用程序中委派本身?我将粘贴一些代码让您理解。将尽快发布(意大利时间14日)@deimus code已添加。。。抱歉迟到了,我很忙。
// Method in the first View that asks to tab the tab bar to launch the other
// view controller

- (void)playVideoPreview {
NSArray *array;

array = [[self tabBarController] viewControllers];
    if ( ![[array objectAtIndex:2] catSwitch] ) {
       [[array objectAtIndex:2] setDelegate:self];
       [[array objectAtIndex:2] setHasBeenLaunchedByDelegate:YES];
    } else {
       [self selectTab];
    }
    [[self tabBarController] setViewControllers:array];
    [[self tabBarController] setSelectedIndex:2];
}

// Now the operations performed by the second View Controller
- (void)somewhereInYourCode {
    if ( hasBeenLaunchedByDelegate ) {
        [[self delegate] selectTab];
    }
}

// In the First View Controller this is the delegate method, 
// launched from the Second View Controller
- (void)selectTab {
    NSArray *array;

array = [[self tabBarController] viewControllers];
    [[[array objectAtIndex:2] catSwitch] setSelectedSegmentIndex:[[bannerPreview pageControl] currentPage]];
}

// Some declaration
@protocol SecondViewControllerDelegate;
class SecondViewController : ViewController {
    id<TGWebViewControllerDelegate> delegate;
}
@end

@protocol SecondViewControllerDelegate 
    - (void)selectTab;
@end

// Meanwhile in the first view
class FirstViewController : ViewController <SecondViewControllerDelegate> {
// ...
}