Eu gostaria de navegar de um controlador de visualização para outro. Como posso converter o seguinte código Objective-C em Swift?
UIViewController *viewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"Identifier"];
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.navigationController pushViewController:navi animated:YES];
Respostas:
Crie um arquivo swift (SecondViewController.swift) para o segundo controlador de visualização e no tipo de função apropriado:
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)
Swift 4
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "IKDetailVC") as? IKDetailVC self.navigationController?.pushViewController(vc!, animated: true)
fonte
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
na segunda linha do seu código. Por favor ajudeEm minha experiência
navigationController
foi nulo, então mudei meu código para este:let next = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardController") as! DashboardController self.presentViewController(next, animated: true, completion: nil)
Não se esqueça de definir ViewController
StoryBoard Id
emStoryBoard
->identity inspector
fonte
Se você não quiser que o botão Voltar apareça (que foi o meu caso, porque eu queria apresentar depois que um usuário fez login), aqui está como definir a raiz do controlador de navegação:
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewController") as! YourViewController let navigationController = UINavigationController(rootViewController: vc) self.presentViewController(navigationController, animated: true, completion: nil)
fonte
SWIFT 3.01
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "Conversation_VC") as! Conversation_VC self.navigationController?.pushViewController(secondViewController, animated: true)
fonte
Em Swift 4.0
var viewController: UIViewController? = storyboard().instantiateViewController(withIdentifier: "Identifier") var navi = UINavigationController(rootViewController: viewController!) navigationController?.pushViewController(navi, animated: true)
fonte
Swift 3
let secondviewController:UIViewController = self.storyboard?.instantiateViewController(withIdentifier: "StoryboardIdOfsecondviewController") as? SecondViewController self.navigationController?.pushViewController(secondviewController, animated: true)
fonte
Em Swift 4.1 e Xcode 10
Aqui, AddFileViewController é o segundo controlador de visualização.
O id do storyboard é 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)
Se necessário, use fio.
Ex:
DispatchQueue.main.async { let next = self.storyboard?.instantiateViewController(withIdentifier: "AFVC") as! AddFileViewController self.present(next, animated: true, completion: nil) }
Se você quiser se mudar depois de algum tempo.
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) }
fonte
In swift 3
let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController self.navigationController?.pushViewController(nextVC, animated: true)
fonte
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let home = storyBoard.instantiateViewController(withIdentifier: "HOMEVC") as! HOMEVC navigationController?.pushViewController(home, animated: true);
fonte
let objViewController = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController self.navigationController?.pushViewController(objViewController, animated: true)
fonte
Swift 4
Você pode mudar a tela pressionando o controlador de navegação primeiro de tudo, você deve definir o controlador de navegação com UIViewController
let vc = self.storyboard?.instantiateViewController(withIdentifier: "YourStoryboardID") as! swiftClassName self.navigationController?.pushViewController(vc, animated: true)
fonte
Swift 5
Use o Segue para realizar a navegação de um View Controller para outro View Controller:
performSegue(withIdentifier: "idView", sender: self)
Isso funciona no Xcode 10.2.
fonte
Em AppDelegate você pode escrever assim ...
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) }
fonte