如何以编程方式在UITabBarController之间切换 [英] How to switch between UITabBarController programmatically

查看:121
本文介绍了如何以编程方式在UITabBarController之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITabBarController的应用程序,其中第一个选项卡包含HomePage的ViewController。我要做的是在标签之间切换(这很简单:[[self tabBarController] setSelectedIndex:index]),AND从HomePage导航到selectedTab的出口。

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".

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

Just to explain myself: TabElement1--->TabElementX---->UISegmentedController:segmentY

问题是UISegmentedController是零因为它尚未初始化(至少我第一次进行操作)。我该如何解决这个问题?标签元素加载了笔尖。

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.
}

现在我第一次触发playVideoPreview我设法进入标签索引2 ,TGWebViewController,但开关还不存在,所以我发现自己的segmentedControl名为开关,选择了第一个段。如果我回到HomeViewController,然后再次触发playVideoPreview,我得到了正确的行为。

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.

推荐答案

我已经解决了问题使用委托和布尔值。现在,当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):

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

这篇关于如何以编程方式在UITabBarController之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆