UIViewController를 초기화 할때는 보통 다음과 같은 방식을 사용합니다.
UIViewController *viewController = [[UIViewController alloc] init];

혹은 NIB를 이용한 다음과 같은 방법도 사용하게 됩니다.
UIViewController *viewController = [[UIViewController alloc] initWithNibName:@"nibName" bundle:[NSBundle mainBundle]];

위와 같이 호출할 경우에는 자동으로 초기화가 되며 loadView, viewDidLoad가 순차적으로 호출되게 됩니다.

그런데 UITabBarController를 Interface Builder를 사용하여 다른 UIViewController를 등록하여 사용하게 되면 위의 두 메서드가 초기화시에 호출되지 않습니다.

또한 비슷한 컨트롤인 UINavigationController로 같은 문제를 가지고 있습니다. 이게 버그인지 무엇인지 잘 모르겠군요.

이런 난감한 문제를 해결하기 위해서 검색을 해보았지만 역시나 IB를 사용하지 않고 초기화 하는 방법밖에 없는 모양입니다.

// UITabBarController 초기화
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.customizableViewControllers = nil;

// 3개의 다른 UIViewController 생성
// 3개의 뷰는 UINavigationView를 사용합니다.
UIViewController *View1 = [[UIViewController alloc] init];
UINavigationController *tab1Controller = [[UINavigationController alloc] initWithRootViewController:View1];
tab1Controller.tabBarItem.image = [UIImage imageNamed:@"1.png"];
[View1 release];

UIViewController *View2 = [[UIViewController alloc] init];
UINavigationController *tab2Controller = [[UINavigationController alloc] initWithRootViewController:View2];
tab2Controller.tabBarItem.image = [UIImage imageNamed:@"2.png"];
[View2 release];

UIViewController *View3 = [[UIViewController alloc] init];
UINavigationController *tab3Controller = [[UINavigationController alloc] initWithRootViewController:View3];
tab3Controller.tabBarItem.image = [UIImage imageNamed:@"3.png"];
[View3 release];

tabBarController.viewControllers = [NSArray arrayWithObjects: tab1Controller, tab2Controller, tab3Controller, nil];

[window addSubview: tabBarController.view];

위와 같은 방법을 사용하시면 정상적으로 작동하는 것을 알 수 있습니다.

참고  : http://discussions.apple.com/thread.jspa?threadID=1670615

이 글의 관련글

트랙백을 보내세요

트랙백 주소 :: http://theeye.pe.kr/trackback/314

댓글을 달아 주세요

  1. 유리별 2009/04/29 16:36

    질문이 있는데요. 위의 코드에서 tab1Controller 등은 따로 release 안해도 되는건가요? View1 만 release 해서 딱 이유가 있는건가 해서요.

    • 아이 2009/04/29 16:47

      안녕하세요. 위의 코드는 완벽한 코드라기 보다는 이런방법이 있다는것을 제시하기 위한 코드라 그대로 옮겨 쓰면 문제가 될수 있는 코드입니다.ㅠㅠ 질문하신 내용을 다시 생각해 보면 NSArray로 배열화 하고 바로 tab1Controller ~ tab3Controller를 release해주셔야 하고 tabBarController.view를 window에 addSubview하신 후에는 [tabBarController.view release] 해주셔야 합니다.

비밀글 (Serect)
댓글 달기 (Submit)