Swift presentViewController
Programowo mam wiele kontrolerów widoku w projekcie iOS Swift. Nie mam storyboardów i chciałbym ich unikać, jeśli to możliwe. Czy istnieje sposób, aby przełączyć się na inny plik viewcontroller.swift
(będziemy go nazywać view2.swift
) i czy będzie on częścią funkcji, którą wywołuje przycisk?
próbowałem:
let storyboard: UIStoryboard = UIStoryboard(name: "myTabBarName", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewControllerWithIdentifier("myVCID") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)
Powyższe działa z storyboardami, ale chcę, aby ktoś inny view2.swift
został wywołany. Czy można to zrobić?
12 answers
Spróbuj tego:
let vc = ViewController() //change this to your class name
self.presentViewController(vc, animated: true, completion: nil)
Z Swift3:
self.present(vc, animated: true, completion: nil)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-29 11:46:35
Dla tych, którzy dostają puste / czarne ekrany ten kod zadziałał dla mnie.
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("myVCId") as! MyVCName
self.presentViewController(vc, animated: true, completion: nil)
Aby ustawić "identyfikator" do twojego VC, po prostu przejdź do inspektora tożsamości dla VC w storyboardzie. Ustaw "Storyboard ID" na to, co chcesz zidentyfikować. Spójrz na poniższy obrazek w celach informacyjnych.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-01 22:21:57
dla odniesienia, ponieważ to pytanie jest jednym z pierwszych wyników Google.
Przełom w Swift 3:
Metodę presentViewController
zastępuje się metodą present
.
Możesz go używać jak starego:
self.present(viewControllerToPresent, animated: true, completion: nil)
Przykład otwarcia kamery:
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-06 08:00:05
Swift 3 i Swift 4
let vc = self.storyboard?.instantiateViewController(withIdentifier: "idMyViewControllerName") as! MyViewControllerName
self.present(vc, animated: true, completion: nil)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-20 08:20:52
Dla mnie miałem dwa widoki w dwóch osobnych kontrolerach nawigacyjnych. Musiałem użyć kombinacji powyższych.
var vc = self.storyboard?.instantiateViewControllerWithIdentifier("WelcomeViewController") as! WelcomeViewController
var navigationController = UINavigationController(rootViewController: vc)
self.presentViewController(navigationController, animated: true, completion: nil)
Swift 3.x
let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "VC-ID" as! yourViewController
let navigationVC = UINavigationController(rootViewController: secondVC)
self.present(navigationVC, animated: true, completion: nil)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-19 09:18:14
Korzystanie Z Swift 2.1+
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("settingsVC") as! SettingsViewController
self.presentViewController(vc, animated: true, completion: nil)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-03 00:49:24
Po prostu użyj tego : Upewnij się, że użycie nibName w przeciwnym razie wstępnie załadowane widoki xib nie będą wyświetlane :
var vc : ViewController = ViewController(nibName: "ViewController", bundle: nil)
//zmień to na nazwę klasy
self.presentViewController(vc, animated: true, completion: nil)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-26 18:04:37
Rozwiązano czarny ekran dodając Kontroler nawigacyjny i ustawiając drugi kontroler widoku jako rootVC.
let vc = ViewController()
var navigationController = UINavigationController(rootViewController: vc)
self.presentViewController(navigationController, animated: true, completion: nil
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-06 23:56:25
Możesz użyć poniższego kodu:
var vc = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewController") as! YourViewController;
vc.mode_Player = 1
self.presentViewController(vc, animated: true, completion: nil)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-29 12:56:25
Inną możliwością jest to, że nie masz XIB zawartego w celu budowania (co się stało ze mną).
Może się to zdarzyć, jeśli masz inny cel dla kompilacji Dev, Test & Release (który powinieneś mieć i tak).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-21 22:18:01
Możesz użyć kodu:
if let vc = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as? secondViewController {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = vc
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-30 03:42:00
Nie musisz tworzyć instancji ViewController w Storyboard tylko po to, aby uruchomić ViewController. To hakerskie rozwiązanie.
Jeśli widzisz Czarny / pusty ekran podczas prezentacji VC, może to być spowodowane wywołaniem present()
z First/Root ViewController. W takim przypadku nie można wywołać present () z viewDidLoad
, ponieważ pierwszy widok nie jest jeszcze gotowy.
Oto poprawka:
-
Wywołaj
present()
zviewDidAppear
Tak:override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) let yourVC = YourViewController() self.present(yourVC, animated: true, completion: nil) }
Once a W Twojej aplikacji pojawił się "widok", możesz zacząć wywoływać present() od
viewDidLoad()
. Użycie
UINavigationController
(zgodnie z sugestią w odpowiedzi) jest inną opcją, ale rozwiązanie tego problemu może być przesadą. Możesz zakończyć się komplikacją przepływu użytkowników. Użyj rozwiązania opartego naUINavigationController
tylko, jeśli chcesz mieć pasek nawigacyjny lub chcesz powrócić do poprzedniego kontrolera widoku.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-18 23:46:35