Me gustaría navegar de un controlador de vista a otro. ¿Cómo puedo convertir el siguiente código Objective-C en Swift?
UIViewController *viewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"Identifier"];
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.navigationController pushViewController:navi animated:YES];
Respuestas:
Cree un archivo rápido (SecondViewController.swift) para el segundo controlador de vista y en la función apropiada escriba esto:
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController self.navigationController.pushViewController(secondViewController, animated: true)
Swift 2+
let mapViewControllerObj = self.storyboard?.instantiateViewControllerWithIdentifier("MapViewControllerIdentifier") as? MapViewController self.navigationController?.pushViewController(mapViewControllerObj!, animated: true)
Rápido 4
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "IKDetailVC") as? IKDetailVC self.navigationController?.pushViewController(vc!, animated: true)
fuente
import UIKit
class SwiftNavigationController: UINavigationController { init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) // Custom initialization }`` override func viewDidLoad() { super.viewDidLoad() }
unexpectedly found nil while unwrapping an Optional value
en la segunda línea de su código. Por favor ayudaEn mi experiencia
navigationController
fue nula, así que cambié mi código a esto:let next = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardController") as! DashboardController self.presentViewController(next, animated: true, completion: nil)
No olvide configurar ViewController
StoryBoard Id
enStoryBoard
->identity inspector
fuente
Si no desea que aparezca el botón Atrás (que era mi caso, porque quería presentar después de que un usuario iniciara sesión), aquí se explica cómo configurar la raíz del controlador de navegación:
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewController") as! YourViewController let navigationController = UINavigationController(rootViewController: vc) self.presentViewController(navigationController, animated: true, completion: nil)
fuente
SWIFT 3.01
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "Conversation_VC") as! Conversation_VC self.navigationController?.pushViewController(secondViewController, animated: true)
fuente
En Swift 4.0
var viewController: UIViewController? = storyboard().instantiateViewController(withIdentifier: "Identifier") var navi = UINavigationController(rootViewController: viewController!) navigationController?.pushViewController(navi, animated: true)
fuente
Swift 3
let secondviewController:UIViewController = self.storyboard?.instantiateViewController(withIdentifier: "StoryboardIdOfsecondviewController") as? SecondViewController self.navigationController?.pushViewController(secondviewController, animated: true)
fuente
En Swift 4.1 y Xcode 10
Aquí AddFileViewController es el segundo controlador de vista.
La identificación del guión gráfico es AFVC
let next = self.storyboard?.instantiateViewController(withIdentifier: "AFVC") as! AddFileViewController self.present(next, animated: true, completion: nil) //OR //If your VC is DashboardViewController let dashboard = self.storyboard?.instantiateViewController(withIdentifier: "DBVC") as! DashboardViewController self.navigationController?.pushViewController(dashboard, animated: true)
Si es necesario, utilice hilo.
Ex:
DispatchQueue.main.async { let next = self.storyboard?.instantiateViewController(withIdentifier: "AFVC") as! AddFileViewController self.present(next, animated: true, completion: nil) }
Si quieres mudarte después de un tiempo.
EX:
//To call or execute function after some time(After 5 sec) DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) { let next = self.storyboard?.instantiateViewController(withIdentifier: "AFVC") as! AddFileViewController self.present(next, animated: true, completion: nil) }
fuente
En Swift 3
let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController self.navigationController?.pushViewController(nextVC, animated: true)
fuente
let objViewController = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController self.navigationController?.pushViewController(objViewController, animated: true)
fuente
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let home = storyBoard.instantiateViewController(withIdentifier: "HOMEVC") as! HOMEVC navigationController?.pushViewController(home, animated: true);
fuente
Rápido 4
Puede cambiar la pantalla presionando el controlador de navegación.En primer lugar, debe configurar el controlador de navegación con UIViewController
let vc = self.storyboard?.instantiateViewController(withIdentifier: "YourStoryboardID") as! swiftClassName self.navigationController?.pushViewController(vc, animated: true)
fuente
Rápido 5
Utilice Segue para realizar la navegación desde un controlador de vista a otro controlador de vista:
performSegue(withIdentifier: "idView", sender: self)
Esto funciona en Xcode 10.2.
fuente
En AppDelegate puedes escribir así ...
var window: UIWindow? fileprivate let navigationCtrl = UINavigationController() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. self.createWindow() self.showLoginVC() return true } func createWindow() { let screenSize = UIScreen.main.bounds self.window = UIWindow(frame: screenSize) self.window?.backgroundColor = UIColor.white self.window?.makeKeyAndVisible() self.window?.rootViewController = navigationCtrl } func showLoginVC() { let storyboardBundle = Bundle.main // let storyboardBundle = Bundle(for: ClassName.self) // if you are not using main application, means may be you are crating a framework or library you can use this statement instead let storyboard = UIStoryboard(name: "LoginVC", bundle: storyboardBundle) let loginVC = storyboard.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC navigationCtrl.pushViewController(loginVC, animated: false) }
fuente