2011-03-01 5 views
0

첫 번째 탭에 HomePage의 ViewController가 포함 된 UITabBarController가있는 응용 프로그램이 있습니다. 필자가해야 할 일은 탭 간을 전환하는 것입니다 (이것은 매우 간단합니다 : [[self tabBarController] setSelectedIndex : index]), 그리고 "HomePage"에서 selectedTab의 아웃렛을 탐색하는 것입니다. 프로그래밍 방식으로 UITabBarController 사이를 전환하는 방법

그냥 자신을 설명하기 : TabElement1 ---> TabElementX ----> UISegmentedController : segmentY

문제는 그것이 내가 할 최소한 처음으로 (아직 초기화되지 않기 때문에 UISegmentedController이 전무하다는 것이다 조작). 이 문제를 어떻게 해결해야합니까? 탭 요소는 닙으로로드됩니다. 내가 탭 인덱스 2, TGWebViewController에 들어가 관리 나는 playVideoPreview 해고 처음

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

자,하지만 스위치는 아직 존재하지 않는, 그래서 나는 나 자신을 찾을 : 여기

EDIT--

는 일부 코드입니다 첫 번째 세그먼트가 선택된 상태에서 "switches"라는 이름의 segmentedControl. HomeViewController로 돌아 가면 playVideoPreview가 다시 실행되고 올바른 동작을 얻습니다.

+0

:

편집 여기에 코드 (이 도움이되기를 바랍니다)입니까? viewDidLoad, viewWillAppear 또는 App Delegate에서? – Ladislav

+0

나는 당신이 이해할 수 있도록 몇 가지 코드를 붙여 넣을 것이다. – IssamTP

답변

0

위임자와 부울을 사용하여 문제를 해결했습니다. 이제 TabBar의 인덱스 2에있는 ViewController의로드가 완료되면 델리게이트에게 어떤 세그먼트를 선택해야하는지 알려주는 메시지를 보냅니다. 당신이 UISegmentedCOntrol이 nil 일이 코드가 있습니까

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

예, 게시하십시오 – deimus

+0

가능한 한 빨리 게시 (이탈리아어 14 시간) – IssamTP

+0

@deimus 코드가 추가되었습니다 ... 늦어서 미안하지만 꽤 바쁩니다. – IssamTP

관련 문제