UINavigation,UiView,ModalView Controller之間的關系


如果當前是個VC,那么就太簡單了,直接就可以push到下一個vc
AddShopViewController *controller = [[AddShopViewController alloc] init];
controller.view.backgroundColor = [UIColor whiteColor];// 設置背景色為白色,消除殘影
[self.navigationController pushViewController:controller animated:YES];
有時候需要從modal view里pushviewcontroller,這當然是不行的,因為你沒有NC,可以NSLog一下試試就知道了。
NSLog(@"ViewControllers before pushing: %@", self.navigationController.viewControllers);
這時你要加某個viewController的view在你的self.view上,你必須做的是:[self addChildViewController:xxx];或者addSubview。因為沒有nav,如果想用nav來控制vc,一般是先有nav,然后把vc加入nav中,如果本身就是個vc那么直接
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:self];
[self presentViewController:navController animated:YES completion:nil];
這樣你的modal就有一個nv,那么你就可以在這個modal里邊用[self.navigationController pushViewController:controller animated:YES];
完整的一個寫法是這樣的,Storyboard表述的很清楚
MyViewController *myViewController = [MyViewController alloc] init];
UINavigationController *navController = [UINavigationController alloc] initWithRootViewController:myViewController];
// Presuming a view controller is asking for the modal transition in the first place.
[self presentViewController:navController animated:YES completion:nil];


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM