如果當前是個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];
