2017-01-01 5 views
-5

정말 이해가 안되기 때문에 다음 코드 블록을 설명 할 수있는 사람이 있는지 궁금합니다. 돌아가UIViewController 및 UIWindow 초기화 이해 이해 도움이 필요합니다.

OtherViewController *ovc = [[ OtherViewController alloc ] initWithNibName:@"OtherViewController" bundle:nil ]; 
[ self.navigationController pushViewController:ovc animated:YES ]; 

이 작업을 수행 : 간단한 단어에

[ self.navigationController popViewControllerAnimated:YES ]; 
+0

특별히 이해하지 못하는 점은 무엇입니까? 스토리 보드가 아닌 XIB 파일을 사용하는 상당히 오래된 학교 코드입니다. 그러나 그것은 나에게 자급 자족스러운 것처럼 보입니다. –

+0

첫 번째 네 줄은 무엇을합니까? 마지막 세 가지는 navigationController를 만들고 viewControllers 사이를 탐색하는 것입니다. – 12345

+0

첫 번째 네 개는 해당 요소와 할당에 대한 생성자/초기화 코드입니다. – mike510a

답변

0

탐색 컨트롤러에는 관리하는보기 컨트롤러 스택의 하단보기 컨트롤러 인 "루트"보기 컨트롤러가 필요합니다.

#1 self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; 
#2 self.navigationController = [[ UINavigationController alloc ] initWithRootViewController:self.viewController ]; 
#3 self.window.rootViewController = self.navigationController; 
[self.window makeKeyAndVisible]; 
#4 [self.window makeKeyAndVisible]; 

줄 1 클래스 "RootViewController"의 뷰 컨트롤러 생성 (사용자 지정보기 컨트롤러 클래스 여야합니다.) 그것은 동일한 이름의 nibfile에서 뷰 컨트롤러의 의견을로드합니다. 이는 instantiateViewControllerWithIdentifier를 사용하여 스토리 보드에서보기 컨트롤러를로드하는 것과 비슷하지만 작성중인보기 클래스 컨트롤러와로드중인 nibfile을 지정해야한다는 것을 제외하고

줄 2는 다음과 같은 네비게이션 컨트롤러를 만듭니다. 새로 생성 된 "RootViewController"는 루트보기 컨트롤러이므로

3 행은 탐색 창을 응용 프로그램 창의 루트보기 컨트롤러로 설치합니다.

4 행은 앱 창을 활성 창으로 만듭니다.

0

설명

self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; 
self.navigationController = [[ UINavigationController alloc ] initWithRootViewController:self.viewController ]; 
self.window.rootViewController = self.navigationController; 
[self.window makeKeyAndVisible]; 

그런 다음 당신은 당신이 할 수있는 새로운 VC를 제시 할 때. 모든 iOS 응용 프로그램에는 적어도 하나의 UIWindow이 있습니다. 항상 UIViewController 개체가 루트로 설정되어 있어야합니다. 이는 화면의 사용자가 볼 수있는 응용 프로그램의 초기 ViewController로 설정한다는 것을 의미합니다. UINavigationController은 ViewControllers를 내부에 밀어 넣는 스택 컨테이너이며이 실속 상태의 맨 위 ViewController는 기본적으로 화면에서만 볼 수 있습니다. 하지만 초기에 UIViewControllerUIWindowUINavigationController's 루트보기 컨트롤러로 모두 루트보기 컨트롤러로 설정해야합니다. 시작점이 필요합니다. UIWindow 루트보기 컨트롤러는 언제든지 변경 될 수 있지만 UINavigationController과 같이 작동하면 루트보기 컨트롤러를 변경할 수 없습니다.

이제 코드에서 어떻게되는지 설명해 드리겠습니다.

self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; 
//In Above Line you are loading a UIViewController from a Xib file name RootViewController.xib into viewController property 
self.navigationController = [[ UINavigationController alloc ] initWithRootViewController:self.viewController ]; 
//In Above Line You are allocating a new navigation controller programatically with a root/initial view controller and you are passing your previously loaded view controller to be set as root view controller of this navigation. 
self.window.rootViewController = self.navigationController; 
//In Above Line You are assigning your navigationController to UIWindow object this means you want your view controllers to be managed in a stack so that if you push a view controller you can snap back easily with a single line of code. 
[ self.navigationController popViewControllerAnimated:YES ]; 
//In This Line you are removing your Top view Controller from a navigation stack Like the Back button does in Setting>General to Setting in iPhone/iPad