2010-12-07 4 views
1

저는 iPhone 용 dual-tabBar 앱을 작성하고 기본 클래스보기 컨트롤러에 다음 코드를 사용하여보기 컨트롤러 안에 여러 개의 탐색 컨트롤러를 추가하려고합니다 (아래 코드 참조). 그러나 문제는 다음과 같습니다. 이전에 초기화되었지만 자체 뷰에 하위 뷰가 추가되지 않습니다. 어떤 아이디어?보기 컨트롤러 안에 여러 탐색 컨트롤러 추가 중?

- (IBAction)ViewButtonPressed:(id)sender 
{ 
    UIButton *b = (UIButton *)sender; 
    int index = b.tag - 1000; 
    [self SelectNavigationController:index]; 
} 

- (void)SelectNavigationController:(int)index 
{ 
    // Set index to top-most view -> 
    UINavigationController *nc = (UINavigationController *)[navigationControllers objectAtIndex:index]; 
    [self.view bringSubviewToFront:nc.view]; 
} 

#pragma mark - 
#pragma mark display 

- (void)Display 
{ 
    CGRect frame = CGRectMake(0, 44, 320, 367); 

    // Create buttons above frame and show navigation controller inside frame -> 

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.origin.y)]; 

    for (int i=0; i<[navigationControllers count]; ++i) 
    { 
     UINavigationController *nc = (UINavigationController *)[navigationControllers objectAtIndex:i]; 
     UIViewController *vc = [nc.viewControllers objectAtIndex:0]; 
     NSString *titel = vc.navigationItem.title; 

     UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [b setBackgroundColor:[UIColor lightGrayColor]]; // TODO: Replace with image <- 
     [b setTitle:titel forState:UIControlStateNormal]; 
     b.tag = i + 1000; 
     [b setFrame:CGRectMake(i * frame.size.width/3, 0, frame.size.width/3, frame.origin.y - 1)]; 
     [v addSubview:b]; 
    } 

    for (int j=0; j<[navigationControllers count]; ++j) 
    { 
     UINavigationController *nc = (UINavigationController *)[navigationControllers objectAtIndex:j]; 
     [nc.navigationBar addSubview:v]; 
     [self.view addSubview:nc.view]; // Add view to view <- 
     nc.view.frame = frame; 
    } 

    [v release]; 

    if (VIEW_DEBUG) 
     NSLog(@"BaseTabViewController.m: self.view.subviews: %d", [self.view.subviews count]); 
} 

#pragma mark - 
#pragma mark addviewcontroller 

- (void)AddViewControllerForNavigationController:(UIViewController *)viewController 
{ 
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
    navController.view.backgroundColor = [UIColor greenColor]; 
    [navigationControllers addObject:navController]; 
    [navController release]; 
} 

#pragma mark - 
#pragma mark init, loadView, viewDidLoad and dealloc 

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    if (self = [super initWithCoder:aDecoder]) 
    { 
     navigationControllers = [[NSMutableArray alloc] init]; 
    } 
    return self; 
} 

- (void)loadView 
{ 
    // 
} 

- (void)viewDidLoad 
{ 
    if (!viewDidLoadAlready) 
    { 
     [self Display]; 
     viewDidLoadAlready = YES; 
     [super viewDidLoad]; 
    } 
} 

그리고 서브 클래스의 코드 :

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    if (self = [super initWithCoder:aDecoder]) 
    { 
     PistKartaViewController *pistKarta = [[PistKartaViewController alloc] init]; 
     pistKarta.navigationItem.title = @"Pistkarta"; 
     LiftRapportViewController *liftRapport = [[LiftRapportViewController alloc] init]; 
     liftRapport.navigationItem.title = @"Liftrapport"; 
     SkipassViewController *skiPass = [[SkipassViewController alloc] init]; 
     skiPass.navigationItem.title = @"Skipass"; 

     [self AddViewControllerForNavigationController:pistKarta]; 
     [self AddViewControllerForNavigationController:liftRapport]; 
     [self AddViewControllerForNavigationController:skiPass]; 

     [pistKarta release]; 
     [liftRapport release]; 
     [skiPass release]; 
    } 
    return self; 
} 
+0

위의 클래스 (첫 번째 코드 블록)는 tabBar 컨트롤러 (xib에서로드 됨) 내부의 UIViewController이므로 initWithCoder-methods입니다. 어떤 도움을 주셔서 다시 한번 감사드립니다! – swebal

+0

정말 비슷한 코드가 필요합니다. 여기에 감사드립니다.하지만 잘 이해하지 못합니다. 소스 코드 링크를 알려주시겠습니까? 도와주세요 . –

답변

0

나는 그것을 알아 냈어. 나는 추가하고있는 뷰 컨트롤러에서 다음을 가졌다.

- (void)loadView 
{ 

} 

물론 이것은 수퍼 클래스가 전혀로드되지 않는다는 것을 의미한다. 바보. 그렇지 않으면,이 방법은 꽤 잘 작동합니다. :)

관련 문제