Je voudrais naviguer d'un contrôleur de vue à un autre. Comment puis-je convertir le code Objective-C suivant en Swift?
UIViewController *viewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"Identifier"];
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.navigationController pushViewController:navi animated:YES];
Réponses:
Créez un fichier swift (SecondViewController.swift) pour le deuxième contrôleur de vue et dans la fonction appropriée, tapez ceci:
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)
la source
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
à la deuxième ligne de votre code. S'il vous plaît aiderD'après mon expérience,
navigationController
c'était nul, alors j'ai changé mon code pour ceci:let next = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardController") as! DashboardController self.presentViewController(next, animated: true, completion: nil)
N'oubliez pas de définir ViewController
StoryBoard Id
dansStoryBoard
->identity inspector
la source
Si vous ne voulez pas que le bouton de retour apparaisse (ce qui était mon cas, car je voulais le présenter après la connexion d'un utilisateur), voici comment définir la racine du contrôleur de navigation:
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewController") as! YourViewController let navigationController = UINavigationController(rootViewController: vc) self.presentViewController(navigationController, animated: true, completion: nil)
la source
SWIFT 3.01
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "Conversation_VC") as! Conversation_VC self.navigationController?.pushViewController(secondViewController, animated: true)
la source
Dans Swift 4.0
var viewController: UIViewController? = storyboard().instantiateViewController(withIdentifier: "Identifier") var navi = UINavigationController(rootViewController: viewController!) navigationController?.pushViewController(navi, animated: true)
la source
Swift 3
let secondviewController:UIViewController = self.storyboard?.instantiateViewController(withIdentifier: "StoryboardIdOfsecondviewController") as? SecondViewController self.navigationController?.pushViewController(secondviewController, animated: true)
la source
Dans Swift 4.1 et Xcode 10
Ici, AddFileViewController est le deuxième contrôleur de vue.
L'identifiant du storyboard est 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 nécessaire, utilisez du fil.
Ex:
DispatchQueue.main.async { let next = self.storyboard?.instantiateViewController(withIdentifier: "AFVC") as! AddFileViewController self.present(next, animated: true, completion: nil) }
Si vous voulez déménager après un certain temps.
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) }
la source
Dans Swift 3
let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController self.navigationController?.pushViewController(nextVC, animated: true)
la source
let objViewController = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController self.navigationController?.pushViewController(objViewController, animated: true)
la source
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let home = storyBoard.instantiateViewController(withIdentifier: "HOMEVC") as! HOMEVC navigationController?.pushViewController(home, animated: true);
la source
Swift 4
Vous pouvez changer d'écran en appuyant sur le contrôleur de navigation tout d'abord, vous devez régler le contrôleur de navigation avec UIViewController
let vc = self.storyboard?.instantiateViewController(withIdentifier: "YourStoryboardID") as! swiftClassName self.navigationController?.pushViewController(vc, animated: true)
la source
Swift 5
Utilisez Segue pour effectuer la navigation d'un View Controller vers un autre View Controller:
performSegue(withIdentifier: "idView", sender: self)
Cela fonctionne sur Xcode 10.2.
la source
Dans AppDelegate, vous pouvez écrire comme ça ...
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) }
la source