Chciałbym przejść z jednego kontrolera widoku do drugiego. Jak mogę przekonwertować następujący kod Objective-C na Swift?
UIViewController *viewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"Identifier"];
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.navigationController pushViewController:navi animated:YES];
Odpowiedzi:
Utwórz plik Swift (SecondViewController.swift) dla drugiego kontrolera widoku iw odpowiedniej funkcji wpisz to:
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)
Szybki 4
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "IKDetailVC") as? IKDetailVC self.navigationController?.pushViewController(vc!, animated: true)
źródło
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
w drugim wierszu kodu. Proszę o pomocZ mojego doświadczenia
navigationController
wynika, że nie ma, więc zmieniłem kod na ten:let next = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardController") as! DashboardController self.presentViewController(next, animated: true, completion: nil)
Nie zapomnij ustawić ViewController
StoryBoard Id
wStoryBoard
->identity inspector
źródło
Jeśli nie chcesz, aby pojawił się przycisk Wstecz (tak było w moim przypadku, ponieważ chciałem przedstawić po zalogowaniu użytkownika), oto jak ustawić root kontrolera nawigacyjnego:
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewController") as! YourViewController let navigationController = UINavigationController(rootViewController: vc) self.presentViewController(navigationController, animated: true, completion: nil)
źródło
SWIFT 3.01
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "Conversation_VC") as! Conversation_VC self.navigationController?.pushViewController(secondViewController, animated: true)
źródło
W szybkim 4.0
var viewController: UIViewController? = storyboard().instantiateViewController(withIdentifier: "Identifier") var navi = UINavigationController(rootViewController: viewController!) navigationController?.pushViewController(navi, animated: true)
źródło
Szybki 3
let secondviewController:UIViewController = self.storyboard?.instantiateViewController(withIdentifier: "StoryboardIdOfsecondviewController") as? SecondViewController self.navigationController?.pushViewController(secondviewController, animated: true)
źródło
W Swift 4.1 i Xcode 10
Tutaj AddFileViewController jest drugim kontrolerem widoku.
Identyfikator scenorysu to 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)
W razie potrzeby użyj gwintu.
Dawny:
DispatchQueue.main.async { let next = self.storyboard?.instantiateViewController(withIdentifier: "AFVC") as! AddFileViewController self.present(next, animated: true, completion: nil) }
Jeśli chcesz się przenieść za jakiś czas.
DAWNY:
//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) }
źródło
Szybko 3
let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController self.navigationController?.pushViewController(nextVC, animated: true)
źródło
let objViewController = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController self.navigationController?.pushViewController(objViewController, animated: true)
źródło
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let home = storyBoard.instantiateViewController(withIdentifier: "HOMEVC") as! HOMEVC navigationController?.pushViewController(home, animated: true);
źródło
Szybki 4
Możesz przełączyć ekran naciskając kontroler nawigacyjny, najpierw musisz ustawić kontroler nawigacyjny z UIViewController
let vc = self.storyboard?.instantiateViewController(withIdentifier: "YourStoryboardID") as! swiftClassName self.navigationController?.pushViewController(vc, animated: true)
źródło
Szybki 5
Użyj Segue, aby przeprowadzić nawigację z jednego kontrolera widoku do innego kontrolera widoku:
performSegue(withIdentifier: "idView", sender: self)
Działa to na Xcode 10.2.
źródło
W AppDelegate możesz pisać w ten sposób ...
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) }
źródło