ナビゲーションスタックからビューをポップしてから、別のビューをまっすぐにプッシュすることは可能ですか?
このセクションにフラットな階層を実装しようとしていますが、セグメント化されたコントローラーを使用したいのですが、セグメント化されたコントローラーを好きなように見せることはできません。そのため、Navigation Controllerを使用しようとしています。
ボタンをクリックすると、次のコードを実行しました。
[[self navigationController] popViewControllerAnimated:YES];
MapsViewController *aViewController = [[MapsViewController alloc]
initWithNibName:@"MapsViewController" bundle:nil];
[self.navigationController pushViewController:aViewController animated:NO];
[aViewController release];
それは大丈夫ですが、プッシュの兆候はありません!任意の助けをいただければ幸いです。
MapsViewController *aViewController = [[MapsViewController alloc]
initWithNibName:@"MapsViewController" bundle:nil];
// locally store the navigation controller since
// self.navigationController will be nil once we are popped
UINavigationController *navController = self.navigationController;
// retain ourselves so that the controller will still exist once it's popped off
[[self retain] autorelease];
// Pop this controller and replace with another
[navController popViewControllerAnimated:NO];//not to see pop
[navController pushViewController:aViewController animated:YES];//to see Push or u can change it to not to see.
または
MapsViewController *aViewController = [[MapsViewController alloc]
initWithNibName:@"MapsViewController" bundle:nil];
UINavigationController *navController = self.navigationController;
//Get all view controllers in navigation controller currently
NSMutableArray *controllers=[[NSMutableArray alloc] initWithArray:navController.viewControllers] ;
//Remove the last view controller
[controllers removeLastObject];
//set the new set of view controllers
[navController setViewControllers:controllers];
//Push a new view controller
[navController pushViewController:aViewController animated:YES];
https://stackoverflow.com/users/1619554/tomer-peled のソリューションから取得したため、他の人がより簡単に見つけることができます。
これはiOS8でそれを行う最良の方法のようです:
UIViewController *newVC = [[UIViewController alloc] init]; // Replace the current view controller
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];
[viewControllers removeLastObject];
[viewControllers addObject:newVC];
[[self navigationController] setViewControllers:viewControllers animated:YES];
Swiftの場合:
let newVc = UIViewController()
var vcArray = self.navigationController?.viewControllers
vcArray!.removeLast()
vcArray!.append(newVc)
self.navigationController?.setViewControllers(vcArray!, animated: false)
NewVcがストーリーボードに存在する場合:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let newVc = storyboard.instantiateViewControllerWithIdentifier("YourViewControllerIdentifier") as! UIViewController
var vcArray = self.navigationController?.viewControllers
vcArray!.removeLast()
vcArray!.append(newVc)
self.navigationController?.setViewControllers(vcArray!, animated: false)
Swift 4:
self.navigationController.setViewControllers[]..
は私にはうまくいきませんでした。しかし、Navigation Controllerをインスタンス変数に保持してPush/Pop操作を行うことで問題を解決できます。したがって、グリッチなしでコントローラを静かに変更できます。
guard let navigationVC = self.navigationController else { return }
navigationVC.popViewController(animated: false)
navigationVC.pushViewController(myNewVC, animated: false)
このコードを使用して、コントローラーをポップまたはプッシュできます。
Objective Cの場合
bool alreadyPushed = false;
//Check if the view was already pushed
NSMutableArray *viewControllers;
if ( (viewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers])) {
for (UIViewController *aViewController in viewControllers) {
if ([aViewController isKindOfClass:[YourControllerName class]]) {
NSLog(@"pop your view controller");
[self.navigationController popToViewController:aViewController animated:YES];
alreadyPushed = true;
break;
}
}
}
//Push Fresh View
if( alreadyPushed == false) {
NSLog(@"Push your view controller");
YourControllerName *YourControllerObject = [[YourControllerName alloc]initWithNibName:@"YourNibName" bundle:nil];
[self.navigationController pushViewController:YourControllerObject animated:YES];
}
Swiftの場合
var alreadyPushed = false
//Check if the view was already pushed
if let viewControllers = self.navigationController?.viewControllers {
for viewController in viewControllers {
if let viewController = viewController as? YourControllerName {
self.navigationController?.popToViewController(viewController, animated: true);
print(" Push Your Controller")
alreadyPushed = true
break
}
}
}
if alreadyPushed == false {
let YourControllerObject = self.storyboard?.instantiateViewControllerWithIdentifier("YourControllerIdentifire") as! YourControllerName
self.navigationController?.pushViewController(YourControllerObject, animated: true)
}
BOOL Present = NO;
fifthViewController * fifthVC = [self.storyboard instantiateViewControllerWithIdentifier:@"homeController"];
for (UIViewController* viewController in self.navigationController.viewControllers) {
//This if condition checks whether the viewController's class is MyGroupViewController
// if true that means its the MyGroupViewController (which has been pushed at some point)
if ([viewController isKindOfClass:[fifthViewController class]] )
{
// Here viewController is a reference of UIViewController base class of MyGroupViewController
// but viewController holds MyGroupViewController object so we can type cast it here
fifthViewController *groupViewController = (fifthViewController*)viewController;
[self.navigationController popToViewController:groupViewController animated:NO];
Present=YES;
}
}
if(Present==NO)
{
[self PushAnimation];
[self.navigationController pushViewController:fifthVC animated:NO];
Present=YES;
}
Swift 3.
誰かがビュー階層に深く入りたい場合:
//Go back to desired viewController and then Push another viewController
var viewControllers = self.navigationController!.viewControllers
while !(viewControllers.last is MyViewControllerClass) {
viewControllers.removeLast()
}
// go to new viewController
let anotherViewController = AnotherViewController(nibName: "AnotherViewController", bundle: nil)
viewControllers.append(anotherViewController)
self.navigationController?.setViewControllers(viewControllers, animated: true)