开发者

How to switch between UITabBarController programmatically

开发者 https://www.devze.com 2023-02-14 14:36 出处:网络
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 tabBarContr

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 w开发者_JS百科ith 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.


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):

// 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

精彩评论

暂无评论...
验证码 换一张
取 消