I get an error:
fatal error: unexpectedly found nil while unwrapping an Optional value
The didTapUserScreenButton() works fine though, the didTapChatControllerButton() gives the error. I'm thinking it might get the error because the didTapChatControllerButton() goes to a UiCollectionViewController? and the other works fine as it's going to a UITableViewController?.
func didTapChatControllerButton() {
let chat_log_controller = ChatLogController
let navController = UINavigationController(rootViewController: chat_log_controller!)
present(navController, animated: true, completion: nil)
}
func didTapUserScreenButton() {
let user_screen_vc = usersScreenVC()
let navController = UINavigationController(rootViewController: user_screen_vc)
present(navController, animated: true, completion: nil)
You missed the (currently empty) initialization argument holder brackets. Try with tris:
let chat_log_controller = ChatLogController()
let navController = UINavigationController(rootViewController: chat_log_controller)
present(navController, animated: true, completion: nil)
Related
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator){
if UIDevice.current.orientation.isPortrait{
let secondView: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let main = secondView.instantiateViewController(withIdentifier: "ViewController") as! ViewController
main.modalPresentationStyle = .fullScreen
self.present(main, animated: true, completion: nil)
}
if UIDevice.current.orientation.isLandscape{
self.dismiss(animated: true, completion: nil)
}
}
I use this code to switch between two different storyboard's viewcontroller. How can I pass data between those viewcontroller in two different storyboard.
I have tried "main.instanceData = myData before the present" It seems could work, but the initial value of myData is nil, so when this line of code was executed, it would show "Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value"
Just try this. You can pass data to viewController directively like that:
let secondView: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let main = secondView.instantiateViewController(withIdentifier: "ViewController") as! ViewController
main.data = yourdata
main.modalPresentationStyle = .fullScreen
self.present(main, animated: true, completion: nil)
I receive this error with the following code. I have a View Controller class called "testViewController". I'm trying to press a button and make it appear, but this error is showing in my code. Help!
self.present(testViewController, animated: true, completion: nil)
I expect this should show the new view controller but it does not.
You need to pass an instance not
let vc1 = TestViewController.self // wrong
let vc2 = TestViewController() // right
self.present(vc2, animated: true, completion: nil) // right
self.present(TestViewController, animated: true, completion: nil) // wrong
self.present(vc1, animated: true, completion: nil) // wrong
Edit: If the vc is inside storyboard load it like
let vc = self.storyboard!.instantiateViewController(withIdentifier: "VCID") as! TestViewController
self.present(vc1, animated: true, completion: nil) // right
I just found code that works. Thanks all. Here it is:
let testViewController = self.storyboard?.instantiateViewController(withIdentifier: "MyTest") as! testViewController // I created MyTest in the identifier field of the identity inspector.
self.present(testViewController, animated: true)
I am running into the issue of my viewcontrollers not showing up even though the function calling the viewcontrollers seem to be running. The error I receive in the console is:
Warning: Attempt to present on whose view is not in the window hierarchy!
I have tried the suggestions on the "Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy" thread without any progress. I am running everything programmatically.
func handleNewPage(){
print("this code works")
let uid = Auth.auth().currentUser?.uid
ref = Database.database().reference()
let usersReference = ref.child("patient").child((uid)!)
if usersReference.child("Doctor Code") != nil {
func presentNewPage(){
let firstPage = LandingPage()
let navCon = UINavigationController(rootViewController: firstPage)
present(navCon, animated: true, completion: nil)
}
presentNewPage()
print("PRINT")
} else{
let newPage = PresentViewController() // doctor reg page
let navController = UINavigationController(rootViewController: newPage)
present(navController, animated: true, completion: nil)
}
}
The function is called and the print statements come out valid. Yet, the viewcontrollers will not appear.
You have to create presentNewPage() function outside of your if ???
if usersReference.child("Doctor Code") != nil {
func presentNewPage(){
let firstPage = LandingPage()
let navCon = UINavigationController(rootViewController: firstPage)
present(navCon, animated: true, completion: nil)
}
presentNewPage()
print("PRINT")
} else{
let newPage = PresentViewController() // doctor reg page
let navController = UINavigationController(rootViewController: newPage)
present(navController, animated: true, completion: nil)
}
change it like this
func presentNewPage(){
let firstPage = LandingPage()
let navCon = UINavigationController(rootViewController: firstPage)
present(navCon, animated: true, completion: nil)
}
func handleNewPage(){
print("this code works")
let uid = Auth.auth().currentUser?.uid
ref = Database.database().reference()
let usersReference = ref.child("patient").child((uid)!)
if usersReference.child("Doctor Code") != nil {
presentNewPage()
print("PRINT")
} else{
let newPage = PresentViewController() // doctor reg page
let navController = UINavigationController(rootViewController: newPage)
present(navController, animated: true, completion: nil)
}
}
let SecondViewController =
self.storyboard?.instantiateViewController(withIdentifier:
"SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(SecondViewController,
animated: true)
For some reason this doesn't work. Any ideas?
Replace
self.navigationController?.pushViewController(SecondViewController,
animated: true)
with
self.navigationController?.present(SecondViewController, animated: true, completion: nil)
Edit: To avoid the optionals you should also do one of the following:
guard let navController = self.navigationController else { return }
navController.present(SecondViewController, animated: true, completion: nil)
or
if let nacVontroller = self.navigationController {
navController.present(SecondViewController, animated: true, completion: nil)
}
Edit2: You should also avoid force unwrapping your SecondViewController using one of the above methods as well. Although that is not your current issue.
I Created Page Based Application from Apples template. I have a function in RootViewController for updating my ModelController after receiving a data from web server:
func updateMC() {
if let mc = getModelControllerWithLatestData() {
self._modelController = mc
let startingViewController: DataViewController = self.modelController.viewControllerAtIndex(1, storyboard: self.storyboard!)!
let viewControllers = [startingViewController]
// self.pageViewController!.setViewControllers(viewControllers, direction: .Forward, animated: false, completion: {done in })
self.pageViewController!.setViewControllers(viewControllers, direction: .Forward, animated: true, completion: nil)
self.pageViewController!.dataSource = self.modelController
self.pageViewController!.didMoveToParentViewController(self)
println("updateMC")
}
}
But new Data appearing on the screen after 10-15 seconds after updating _modelController.
How can I fix the issue?
P.S. I posted src of the RootViewController, hope this would be helpful: https://gist.github.com/stillfinder/d3ec7a57c73569411ff5
That happened because a background thread can't modify the UI. Only the UI Thread should modify that add this code, to run the modifiers in the main thread
dispatch_async(dispatch_get_main_queue()) {}
func updateMC() {
if let mc = getModelControllerWithLatestData() {
dispatch_async(dispatch_get_main_queue()) {
self._modelController = mc
let startingViewController: DataViewController = self.modelController.viewControllerAtIndex(1, storyboard: self.storyboard!)!
let viewControllers = [startingViewController]
// self.pageViewController!.setViewControllers(viewControllers, direction: .Forward, animated: false, completion: {done in })
self.pageViewController!.setViewControllers(viewControllers, direction: .Forward, animated: true, completion: nil)
self.pageViewController!.dataSource = self.modelController
self.pageViewController!.didMoveToParentViewController(self)
println("updateMC")
}
}
}