Tag Archives: UINavigationController

[iPhone] UINavigationController 정리

UINavigationController를 사용할때 쓸만한 내용을 발견하여 정리해 봅니다.

1. 초기화
UINavigationController의 초기화를 위해서는 보통 가장 루트가 되는 뷰컨트롤러를 지정하게 됩니다.

[code]UIViewController *viewController = [[UIViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[/code]

2. 새로운 뷰로 이동하기/빠져 나오기
새로운뷰로 이동하는것은 다음과 이해하시면 좋을것 같습니다. 네비게이션컨트롤러는 기본적으로 스택(Stack)과 같은 구조로 동작을 하며, 눈에 보이는 뷰는 항상 가장 마지막에 삽입된 객체(뷰)가 됩니다.

그래서인지, 뷰를 삽입하고 빼는 메서드들의 이름이 push/pop으로 시작을 하는군요.

[code]// 새로운 뷰 삽입하기
[navController pushViewController:newViewController animated:YES];
 
// 뷰컨트롤러 안에서 – 자기 자신을 네비게이션 컨트롤러에서 제거
[self.navigationController popViewControllerAnimated:YES];
 
// 어디서든지 네비게이션 컨트롤러에 접근 가능할 때
[navController popViewControllerAnimated:YES];[/code]

3. 최상위 뷰로 한번에 이동하기
여러개의 뷰를 통해서 들어가 있는 상태라면 다음과 같은 방법으로 한번에 최상위로 빠져나갈 수 있습니다.

[code][self.navigationController popToRootViewControllerAnimated:YES];[/code]

4. 모달(Modal)뷰 형식으로 띄우기
네비게이션 컨트롤러를 사용하긴 하지만 페이징의 느낌이 들지 않게 별개의 페이지처럼 띄우고 싶을 경우가 있습니다. 다음의 메서드를 사용하여 상단의 네비게이션바조차 나오지 않는 풀스크린의 뷰를 띄울 수 있습니다.

[code]// 모달 뷰 띄우기
[self.navigationController presentModalViewController:modelViewController animated:YES];
 
// 모달 뷰 제거 – 모달 뷰 컨트롤러 내부에서
[self dismissModalViewControllerAnimated:YES];[/code]

참고 : http://qstufie.com/blog/bb/2008/10/uinavigationcontroller-how-to/

[iPhone] UITabBarController 이용시 UIViewController의 viewDidLoad가 호출되지 않는 문제 해결

UIViewController를 초기화 할때는 보통 다음과 같은 방식을 사용합니다.
[code]UIViewController *viewController = [[UIViewController alloc] init];[/code]
혹은 NIB를 이용한 다음과 같은 방법도 사용하게 됩니다.
[code]UIViewController *viewController = [[UIViewController alloc] initWithNibName:@”nibName” bundle:[NSBundle mainBundle]];[/code]
위와 같이 호출할 경우에는 자동으로 초기화가 되며 loadView, viewDidLoad가 순차적으로 호출되게 됩니다.

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

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

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

[code]// 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];[/code]
위와 같은 방법을 사용하시면 정상적으로 작동하는 것을 알 수 있습니다.

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