2011-03-01 75 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

相關問題