Controlador de navegación Controlador de vista push

Pregunta

¿Cómo navegar desde un controlador de vista a otro simplemente usando el toque dentro de un evento?

Más información

Lo que probé en un proyecto de muestra, en pasos, fue:

  1. Cree la aplicación de muestra de vista única.

  2. Agregue un nuevo archivo -> Clase Objective-C con XIB para la interfaz de usuario (ViewController2).

  3. Agregue un botón en ViewController.xib y controle el clic en el botón ViewController.h para crear el evento retoque adentro.

  4. Vaya a IBAction recién creado en ViewController.m y cámbielo a este …

    - (IBAction)GoToNext:(id)sender { ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil]; [[self navigationController] pushViewController:vc2 animated:YES]; } 

El código se ejecuta sin errores y probé la funcionalidad del botón con NSLog. Sin embargo, todavía no me lleva al segundo controlador de vista. Cualquier ayuda sería apreciada.

Swift3

  **Push** 

Gustar

 let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as NewsDetailsViewController vc.newsObj = newsObj navigationController?.pushViewController(vc, animated: true) 

o más seguro

  if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController { viewController.newsObj = newsObj if let navigator = navigationController { navigator.pushViewController(viewController, animated: true) } } 

presente

  let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = self.storyboard?.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as! NewsDetailsViewController vc.newsObj = newsObj present(vc!, animated: true, completion: nil) 

o más seguro

  if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController { vc.newsObj = newsObj present(vc, animated: true, completion: nil) } //Appdelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController]; self.window.rootViewController = navigation; [self.window makeKeyAndVisible]; return YES; } //ViewController.m - (IBAction)GoToNext:(id)sender { ViewController2 *vc2 = [[ViewController2 alloc] init]; [self.navigationController pushViewController:vc2 animated:YES]; } 

rápido

 //Appdelegate.swift func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { self.window = UIWindow(frame: UIScreen.mainScreen().bounds) let navigat = UINavigationController() let vcw = ViewController(nibName: "ViewController", bundle: nil) // Push the vcw to the navigat navigat.pushViewController(vcw, animated: false) // Set the window's root view controller self.window!.rootViewController = navigat // Present the window self.window!.makeKeyAndVisible() return true } //ViewController.swift @IBAction func GoToNext(sender : AnyObject) { let ViewController2 = ViewController2(nibName: "ViewController2", bundle: nil) self.navigationController.pushViewController(ViewController2, animated: true) } 
 UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"storyBoardName" bundle:nil]; MemberDetailsViewController* controller = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentiferInStoryBoard"]; [self.navigationController pushViewController:viewControllerName animated:YES]; 

Para Swift utiliza el siguiente código:

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window!.backgroundColor = UIColor.whiteColor() // Create a nav/vc pair using the custom ViewController class let nav = UINavigationController() let vc = NextViewController(nibName: "NextViewController", bundle: nil) // Push the vc onto the nav nav.pushViewController(vc, animated: false) // Set the window's root view controller self.window!.rootViewController = nav // Present the window self.window!.makeKeyAndVisible() return true } 

ViewController:

  @IBAction func Next(sender : AnyObject) { let nextViewController = DurationDel(nibName: "DurationDel", bundle: nil) self.navigationController.pushViewController(nextViewController, animated: true) } 

Usando este código para Navegar en el siguiente controlador de vista, si está usando el guión gráfico, siga este código a continuación,

 UIStoryboard *board; if (!self.storyboard) { board = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; } else { board = self.storyboard; } ViewController *View = [board instantiateViewControllerWithIdentifier:@"yourstoryboardname"]; [self.navigationController pushViewController:View animated:YES]; 

Use este código en su acción de botón (Swift 3.0.1):

 let vc = self.storyboard?.instantiateViewController( withIdentifier: "YourSecondVCIdentifier") as! SecondVC navigationController?.pushViewController(vc, animated: true) 

UINavigationController no se presenta automáticamente en UIViewController.

Esto es lo que debería ver en Interface Builder. El propietario de los archivos tiene una salida de visualización al controlador de navegación y desde el controlador de navegación se sale a la vista real;

Constructor de interfaz

Esto está funcionando perfecto:

PD: recuerde importar el VC de destino:

 #import "DestinationVCName.h" - (IBAction)NameOfTheAction:(id)sender { DestinationVCName *destinationvcname = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationVCName"]; [self presentViewController:destinationvcname animated:YES completion:nil]; } 
 - (void) loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error{ UINavigationController *nav = [self.storyboard instantiateViewControllerWithIdentifier:@"nav"]; ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"LoggedInVC"]; [nav pushViewController:vc animated:YES]; [self presentViewController:nav animated:YES completion:nil]; } 

“nav” es la ID Storyboard para mi controlador de navegación “vc” es la ID Storyboard para mi primer controlador de vista conectado a mi controlador de navegación

-espero que esto ayude

Si está usando Swift:

 let controller = self.storyboard!.instantiateViewControllerWithIdentifier("controllerID") self.navigationController!.pushViewController(controller, animated: true) 
 UIViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboardId"]; [self.navigationController pushViewController:vc animated:YES]; 

AppDelegate a ViewController:

 let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let loginPageView = mainStoryboard.instantiateViewControllerWithIdentifier("leadBidderPagerID") as! LeadBidderPage var rootViewController = self.window!.rootViewController as! UINavigationController rootViewController.pushViewController(loginPageView, animated: true) 

Entre ViewControllers:

 let loginPageView = self.storyboard?.instantiateViewControllerWithIdentifier("scoutPageID") as! ScoutPage self.navigationController?.pushViewController(loginPageView, animated: true) 

Primero crea el controlador de navegación y proporciónalo como rootViewController de tu objeto de ventana.