UICollectionView is nil - ios

I have a UIViewController in storyboard which hosts a collectionView which is referenced using a IBOutlet. However, now I am placing this inside of a PageViewController and referencing it like this, rather than with the use of a seque :
let initial = FeedCollectionViewController()
let viewControllers = [initial]
setViewControllers(viewControllers, direction: .Forward, animated: true, completion: nil)
The problem is that the collectionView is now nil.

Of course it's nil. You built your view controller in storyboard, so you should init it from storyboard. Like this:
let tabSb = UIStoryboard(name: "Main", bundle: nil)
let tabbarVc = tabSb.instantiateInitialViewController()

Related

Display navigation when a viewController is displayed programatically

I'm using https://github.com/jonkykong/SideMenu for my Swift 4 application. It's working good, but I have problems to display a new ViewController programmatically and the NavigationController.
If I use:
let vc = MyViewController.self() //your view controller
self.present(vc, animated: true, completion: nil)
All the IBOutlet are nil and the ViewController is not loaded.
And the same happens with:
let vc = MyViewController()
self.navigationController?.pushViewController(vc, animated: true)
How I can display a new ViewController keeping the Navigator of the SideMenu?
If you are not creating the VC's view programmatically in code , you have to load it either from storyboard
let yourVC = storyboard.instantiateViewController(withIdentifier: "YourVC") as! YourVC
or nib
let yourVC = YourVC(nibName: "YourVCNibName", bundle: nil)

Create a new view with swift

I am new with iOS an in this website. I was using a navigation controller and pushViewController to switch to other views. But now I am trying to push to a view without a navigation bar. The new view should contain a Done button which once clicked should bring the user back to the last view. Do you have an idea how to achieve that? I tried this
let storyboard = UIStoryboard(name: "Main.Storyboard", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "FormViewController")
self.present(controller, animated: true, completion: nil)
I got an error
'Could not find a storyboard named 'Main.storyboard' in bundle NSBundle. I get the same error if I try FormViewController as the story board name. I set FormViewController as Storyboard ID and also as the swift file name I want to use with the interface, and Main.Storyboard is the name of the storyboard file.
You don't add the ".storyboard" part of the filename, it does that automatically:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
When dealing with the storyboard and accessing ViewController, you will have to follow these steps:
1. Assign Storyboard ID to ViewController
In your Storyboard, select the desired ViewController and assign Storyboard Id to it
2. Access the ViewController
To access the ViewController, you will need the Storyboard Id which we have set in the first step.
If you have only one storyboard you can simply do like this
let controller = self.storyboard.instantiateViewController(withIdentifier: "YourViewControllerID")
self.present(controller, animated: true, completion: nil)
**
OR
**
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "YourViewControllerID")
self.present(controller, animated: true, completion: nil)
If you are using multiple storyboards within the same project then you will have to access storyboard with it's name (without its extension i.e .Storyboard):
let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "YourViewControllerID")
self.present(controller, animated: true, completion: nil)

Showing a specific view controller on load

In some cases I want my app to present a different view controller than the one that normally loads (in this case AltView rather than MainView), but I can't figure out how to do it. I tried like this:
let storyboard = UIStoryboard.init(name: "Alt", bundle: nil)
let nav = storyboard.instantiateViewControllerWithIdentifier("AltView")
let mainStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let rootVC = mainStoryboard.instantiateViewControllerWithIdentifier("MainView")
self.window!.rootViewController = rootVC
window!.makeKeyAndVisible()
self.window!.rootViewController?.presentViewController(nav, animated: true, completion: nil)
But nothing happens, the screen is just blank, no errors or warnings are being reported.
EDIT: AltView is an UINavigationController with an embedded UIViewController. The viewDidLoad() method of AltView's embedded view controller is being run, but its viewWillAppear() method is not.
What am I doing wrong?
Just set the window controller like this
let mainStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let nav = storyboard.instantiateViewControllerWithIdentifier("AltView")
self.window!.rootViewController = nav
If u want to change the controller change the window rootviewcontroller again with same code just change the identifier to the one that you want of the controller and you don't need this statement `self.window!.rootViewController?.presentViewController(nav, animated: true, completion: nil)
If you face any issues let me know

How do I transition to a different view controllers present in different storyboards programmatically using swift?

let customerStoryboard = UIStoryboard(name: "Customer", bundle: nil) //First(Current) storyboard
let agentStoryboard = UIStoryboard(name: "Agent", bundle: nil) //Second storyboard
var otherVC = UIViewController! // ViewController reference
otherVC = agentStoryboard.instantiateViewControllerWithIdentifier("AgentProfile")
The last statement is not executing or showing results, is anything else required to do other than this?
You only instantiate view controller. To present it you should use presentViewController method
someVC.presentViewController(otherVC, animated: true, completion: nil)
Use this code to show the second view controller.
let otherVC : AnyObject! = self.agentStoryboard.instantiateViewControllerWithIdentifier("AgentProfile")
self.showViewController(otherVC as UIViewController, sender: otherVC)
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.navigationController.pushViewController(secondViewController, animated: true)

How to load UIViewController programmatically from storyboard?

I created a UIViewController in my Main.storyboard with a few buttons and labels. I'm trying to switch to that view controller using self.presentViewController but it will not load the view from storyboard. It will only load a blank black screen by default. Any idea on how to load the view from what i've created in storyboard?
self.presentViewController(ResultViewController(), animated: true, completion: nil)
The way you're doing this just creates a new instance of your view controller. It does not create one from the prototype you've defined in Interface Builder. Instead, you should be using this, where "SomeID" is a storyboard ID that you've assigned to your view controller in Interface Builder.
if let resultController = storyboard!.instantiateViewControllerWithIdentifier("SomeID") as? ResultViewController {
presentViewController(resultController, animated: true, completion: nil)
}
You can assign a storyboard ID to your view controller in Interface Builder's identity inspector.
Full Swift 3 code including instantiation of Storyboard:
let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
if let mainViewController = storyboard.instantiateInitialViewController() {
present(mainViewController, animated: false, completion: nil)
}
SWIFT 5.2
first, you should define a storyboardID for viewcontroller and after use this code
let storyboard = UIStoryboard(name: "Main", bundle: nil) // type storyboard name instead of Main
if let myViewController = storyboard.instantiateViewController(withIdentifier: "myViewControllerID") as? CourseDetailVC {
present(myViewController, animated: true, completion: nil)
}

Resources