Ответ 1
Предположим, что мы находимся в
FirstViewController
//Obj-C
- (void) presentSecondVC {
SecondViewController *vc = [[SecondViewController alloc] init];
[self addChildViewController:vc];
[self didMoveToParentViewController:vc];
}
//Swift
func presentSecondVC() {
let vc = SecondViewController.init()
self.addChildViewController(vc)
self.didMove(toParentViewController: vc)
}
Некоторым из вас может потребоваться написать выше метод, подобный этому,
//Obj-C
- (void) presentSecondVC {
SecondViewController *vc = [[SecondViewController alloc] init];
vc.view.frame = CGRectMake(0,0,width,height); //Your own CGRect
[self.view addSubview:vc.view]; //If you don't want to show inside a specific view
[self addChildViewController:vc];
[self didMoveToParentViewController:vc];
//for someone, may need to do this.
//[self.navigationController addChildViewController:vc];
//[self.navigationController didMoveToParentViewController:vc];
}
//Swift
func presentSecondVC() {
let vc = SecondViewController.init()
vc.view.frame = CGRect.init(x: 0, y: 0, width: width, height: height) //Your own CGRect
self.view.addSubview(vc.view) //If you don't want to show inside a specific view.
self.addChildViewController(vc)
self.didMove(toParentViewController: vc)
//for someone, may need to do this.
//self.navigationController?.addChildViewController(vc)
//self.navigationController?.didMove(toParentViewController: vc)
}
Теперь в
SecondViewController
, когда вы хотите вернуться назад
//Obj-C
- (void) goBack {
[self removeFromParentViewController];
}
//Swift
func goBack() {
self.removeFromParentViewController()
}
Хорошо играть (с каждым сценарием):)
И да, это не покажет анимацию, в моем случае я показываю пользовательское всплывающее окно внутри vc
, хотя он выглядит хорошо с этим кодом!