I'm trying to use a Customizable PageMenuViewController in Swift from cocoapods :Link
I want to load my already made views but all I get is a blank page
this is the function to load views:
func viewControllers(forPageMenuController pageMenuController: PageMenuController) -> [UIViewController] {
let detailVC = HomeViewController()
let vc : UIViewController = detailVC as UIViewController
let detailVC1 = SearchViewController()
let vc1 : UIViewController = detailVC1 as UIViewController
return [vc ,vc1 , vc ,vc1 ]
}
and when I used a ViewController that contained a table view I get this error :
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
Error
can someone help me please ?
I initiated my ViewControllers by storyboard and it worked. so instead of :
let detailVC = HomeViewController()
let vc : UIViewController = detailVC as UIViewController
I wrote :
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "HomeViewController")
and it worked but the page is still white nothing changed.
thank you Alexander
Related
I am trying to navigate from one Storyboard to another Storyboard. Here is my code -
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "detail") as! detailsArticle
controller.jsonData = json2["post"]
self.navigationController!.pushViewController(controller, animated: true)
However, when I try to navigate after clicking the button which runs the above code, I get exception error:
Fatal error: Unexpectedly found nil while unwrapping an Optional value: file
How do I solve this?
There are 2 places you could be getting the crash.
The identifier of the detailsArticle is not set as "detail".
The controller you're trying to push onto is not embedded in navigationController.
Try running and check the console to see the debug messages I've added:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let controller = storyboard.instantiateViewController(withIdentifier: "detail") as? detailsArticle else { print("detail vc id not set"); return }
controller.jsonData = json2["post"]
guard let navigationController = navigationController else { print("this vc is not embedded in navigationController"); return }
navigationController.pushViewController(controller, animated: true)
From the comments it's the 2nd one causing the issue. To fix it here are the steps:
Go to <StoryboardName>.storyboard where the current UIViewController sub-class is located and select the current UIViewController scene.
From the Menu bar choose Editor and Embedd In and select Navigation Controller.
I have a UIView set to hidden on a View Controller (product menu), then the user clicks on a product via tableview cell. When they hit return to the initial View Controller I want to make the UIView in the first VC visible.
When I use the code below I get the error: Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
{
//First View Controller
#IBOutlet weak var basketView: UIView!
.
.
self.basketView?.isHidden = true
//Second View Controller
#IBAction func returnBtn(_ sender: UIButton) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let menuCV = storyBoard.instantiateViewController(withIdentifier: "MenuViewController") as! MenuViewController
menuCV.basketView.isHidden = false
self.present(menuCV, animated: true, completion: nil)
}
}
You can't access any outlet before the vc is presented/loaded as i'll be nil so
Option 1
menuCV.loadViewIfNeeded()
menuCV.basketView.isHidden = false
Option 2
Add a bool value
menuCV.hideBasket = false
Then set this inside viewDidLoad of MenuViewController
self.basketView.isHidden = hideBasket
I am trying to access the objects from child controller but it always returns nil. Please check the below code.
let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let vc: UITabBarController = mainStoryboard.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController
vc.selectedIndex = 2
let vc1 = vc.viewControllers?[2] as? FormViewController //this line returns nil
vc1?.fillUserData(dataDic: convertJsonStringToDictionary(jsonString: decodedURL))
vc1?.formViewDelegate = self
self.present(vc, animated: true, completion: nil)
Please shed some light.
Based on your comments, the 3rd tab is actually a UINavigationController which has the FormViewController as its rootViewController.
Update your code as:
if let nc = vc.viewControllers?[2] as? UINavigationController, let vc1 = nc.topViewController as? FormViewController {
vc1.fillUserData(dataDic: convertJsonStringToDictionary(jsonString: decodedURL))
vc1.formViewDelegate = self
}
You can try
let nav = vc.viewControllers?[2] as? UINavigationController
let vc1 = nav?.topViewController as? FormViewController
note : you should not access any UI element here
vc1?.fillUserData(dataDic: convertJsonStringToDictionary(jsonString: decodedURL))
as it would crash the app
I have 3 items in my storyboard. 1. viewController (A) connected to 2. Navigation controller and 3. viewController (B) is NOT connected to anything
All 3 items have restoration identifiers set in the storyboard.
viewController (B) is the initial view controller.
I am trying in B to get the navigation controller that A is attached to, but always returns nil:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewControllerWithIdentifier("viewControllerA") as! ViewControllerA
print(vc.navigationController?) // always prints nil
why!?
UPDATE#1
I can't declare a UINavigationController like a view controller. I've tried setting navigationcontroller with the id of 'myNavigationController' as storyboardID:
When storyboard, I get this error
let navigationController = storyBoard.instantiateViewControllerWithIdentifier("myNavigationController") as! UINavigationController
print(self.navigationController!) // fatal error: unexpectedly found nil while unwrapping an Optional value
I've also tried setting the id in restoration identifier, It bombed earlier at the instantiating line
#ColdLogic, see what hits I get when I search the entire project for that identifier:
Because you never instantiated the navigation controller, you instantiated view controller A. You would want something like this if you want both the nav controller and the view controller to be setup like they are in the storyboard
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = storyBoard.instantiateViewControllerWithIdentifier("YourNavControllerIdentifier") as! UINavigationController
let vc = navigationController.topViewController as! ViewControllerA
Your code directly instantiates an object of type ViewControllerA. Which, unless you setup logic to do it, does not have a navigation controller by default.
This Worked for me!
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = storyBoard.instantiateViewController(withIdentifier: "HomeNavigationContoller") as! UINavigationController
let viewController = storyBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
navigationController.pushViewController(messageVC, animated: true)
self.present(navigationController, animated: true, completion: nil)
Use NavigationController identifier to access the NavigationController and ViewController A is already attached to it, so it will automatically get loaded to NavigationController
I have a UISplitViewController in my iOS app, but my initial view controller is a normal UIViewController.
I want to open the UISplitViewController when a button is clicked on the UIViewController:
#IBAction func openSplitViewController(sender: AnyObject) {
let splitViewController = UISplitViewController()
let leftNavController = splitViewController.viewControllers.first as! UINavigationController
let masterViewController = leftNavController.topViewController as! MenuTableViewController
let rightNavController = splitViewController.viewControllers.last as! UINavigationController
let detailViewController = rightNavController.topViewController as! DetailViewController
splitViewController.viewControllers = [masterViewController,detailViewController];
self.presentViewController(splitViewController, animated: true, completion: nil)
}
But when I click the button, I get:
fatal error: unexpectedly found nil while unwrapping an Optional value
How should I be opening the UISplitViewController from the UIViewController?
Your error has nothing to do with a UISplitViewController, the error you get is because one or more of your arguments is nil.
So make sure that all of the viewControllers you create are real and not nil.
Look for instance at this line:
let leftNavController = splitViewController.viewControllers.first as! UINavigationController
At this point your splitViewController has no viewControllers assigned, so leftNavController will be nil. You will need to create the navController and viewController before adding them to your splitViewController.