2011-03-01 76 views
0

我有一个应用程序与UITabBarController,其中第一个选项卡包含一个主页的ViewController。我必须做的是在标签之间切换(这很简单:[[self tabBarController] setSelectedIndex:index]),AND从“HomePage”中通过selectedTab的出口导航。如何以编程方式在UITabBarController之间切换

只是为了解释自己:TabElement1 ---> TabElementX ----> UISegmentedController:segmentY

的问题是,UISegmentedController是零,因为它尚未初始化(至少是我第一次做操作)。我应该如何解决这个问题?选项卡元素装有笔尖。

EDIT--下面是一些代码:

@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,我会得到正确的行为。

+0

你在哪里有这样的代码,其中UISegmentedCOntrol是零?在viewDidLoad中,viewWillAppear或App Delegate本身? – Ladislav 2011-03-01 10:18:15

+0

我会粘贴一些代码让你理解。 – IssamTP 2011-03-01 10:46:24

回答

0

我已经解决了使用委托和布尔值的问题。现在,当TabBar的索引2处的ViewController加载完成时,它会向其代理发送一条消息,告知哪些段必须被选中。

编辑下面的代码(希望它可以帮助):

// 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> { 
// ... 
} 
+0

yup,请发帖 – deimus 2011-04-11 10:54:04

+0

将尽快发布(14意大利时间) – IssamTP 2011-04-12 07:30:32

+0

@deimus代码添加...对不起,但我很忙,但我很忙。 – IssamTP 2011-04-22 10:14:00

相关问题