Add storyboard in Swift - swift

How do you create multiple storyboards in the same project using Swift? I know I have to go to File -> New -> File and then choose Storyboard, but how do I integrate that storyboard into my project and navigate to the new storyboard's views in Swift code?

You need to get a reference to the desired storyboard
let aStoryboard = UIStoryboard(name: "storyboardName", bundle: nil)
Then instantiate desired view controller
let vc = aStoryboard.instantiateViewControllerWithIdentifier("viewControllerIdentifier") as! UIViewController

Related

How do I pop out a ViewController(like a modal) programmatically?

All:
I am doing a swift , cocoa macos app project with multiple ViewControllers in one storyboard.
I know if I do segue and link the segue from second ViewController to first ViewController.
I can pop out ViewController.
But what if I have a function and want to call another ViewController to present programmatically from first ViewController?
I search a lot of examples start with UIStoryBoard, but my Storyboard is NSStoryboard.
Could anyone hint me a little to start with?
my code:
func checkPassword(SystemMsg:String) -> String{
//print("x")
let storyBoard = NSStoryboard(name: "Main", bundle: nil)
let mainViewController : NSViewController = storyBoard.instantiateController(withIdentifier: "PasswordInput") as! NSViewController
//self.present(mainViewController, asPopoverRelativeTo: <#T##NSRect#>, of: sender, preferredEdge: NSRectEdge, behavior: <#T##NSPopover.Behavior#>)
return ""
}
And my viewController in storyboard look like(no segue,no link):
enter image description here
If anyone can guide me through this step by step would be appreciated.
I figure it out myself.
The most simple way contains 4 steps:
Identify your main ViewController and second ViewController in storyboard
Create your main ViewController with instantiateController
Create your second ViewController with instantiateController
use presentAsModalWindow or presentAsSheet to present secondController on main ViewController
first we need to Identify storyboard correctly:
Identify first storyboard , we need to click top blue cube and then name it in Storyboard Id area
Identify second storyboard , we need to click top blue cube and then name it in Storyboard Id area
example code:
func checkPassword(SystemMsg:String) -> String{
//print("x")
//let storyBoard = NSStoryboard(name: "Main", bundle: nil)
let mainViewController : NSViewController = self.storyboard?.instantiateController(withIdentifier: "MainController") as! NSViewController
let passwordInputViewController : NSViewController = self.storyboard?.instantiateController(withIdentifier: "PasswordInput") as! NSViewController
mainViewController.presentAsModalWindow(passwordInputViewController)
//Or
mainViewController.presentAsSheet(passwordInputViewController)
return ""
}
If I miss something please correct me gently.
reference: Transitioning between view controller, OS X
PS. if you want to pass value between ViewController, this is good reference : https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/#back-properties

How to call XIB in my existing XIB?

Description:- I am coding for a custom framework, For this I am using XIB everywhere, Now I want to call another XIB in my existing XIB, Its calling very fine But unable to perform button action of called XIB. Thanks in advance Here is code.
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "WelcomeVC", bundle: bundle)
self.contentView = nib.instantiate(withOwner: self, options: nil).first as! UIView
self.addSubview(self.contentView)
MVC - is global iOS concept. Learn it again.
xib - is view "V", "V" - cant been as controller "C".
if you can call xib - make it in a controller.

Casting failure with a view controller instantiated from a storyboard

I have a view controller that instantiates a new window controller.
I need to pass an object to that window controller's view controller.
Here is what I have so far:
let storyboard = NSStoryboard(name: "Main", bundle: nil)
if let windowController = storyboard.instantiateController(withIdentifier: "customerEditWindowController") as? NSWindowController
{
let viewController = windowController.contentViewController as! EditCustomerViewController
viewController.customer = customer // (An object)
windowController.window?.makeKeyAndOrderFront(self)
}
The window is displayed as expected and then Xcode traps into the debugger with the error:
Could not cast value of type 'NSViewController' (0x7fffd1dcc4e0) to
'Inventory2.EditCustomerViewController' (0x100011660).
I'm confused since my EditCustomerViewController extends NSViewController.
class EditCustomerViewController: NSViewController
1) Why can't NSViewController be cast to EditCustomerViewController?
2) Is there a better way to get data into the new view controller?
This is a simple shot in the dark but did you set the view controller’s class name in Interface Builder?

Cannot Programmatically Switch Storyboards

I'm attempting to switch storyboards when a button only if a condition (The textfield not being empty) is met. I found this piece of code as an answer to a similar question, and it doesn't seem to be working. My program crashes with "'Storyboard () doesn't contain a view controller with identifier 'Avatar Controller''"
I'm not completely sure as to where to find the view controller identifies, or whether or not I have to add '.storyboard' at the end of the storyboard name.
let storyboard = UIStoryboard(name: "Avatar", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "Avatar Controller")
self.navigationController!.pushViewController(vc, animated: true)
To make it works, Use Storyboard ID has to be checked
This could be happening for 2 reasons.
1) Because in your Avatar.storyboard you are referencing a view controller with the id Avatar Controller but you have not set that value on any of the view controllers in the storyboard file. Go into your Avatar.storyboard, click on the view controller you want to show and go to this panel:
Make sure that the Storyboard ID for your viewController matches what value you use in storyboard.instantiateViewController(withIdentifier: "Avatar Controller")
2) Because you are not providing a bundle when you are loading your storyboard. You are calling this
let storyboard = UIStoryboard(name: "Avatar", bundle: nil);
but because you specify bundle as nil it doesn't know where to look for the Avatar.storyboard file. To fix it, specify bundle as Bundle.main:
let storyboard = UIStoryboard(name: "Avatar", bundle: Bundle.main);

Dynamic Popups in Swift on label word selection

I'm looking for a way to show a dynamically show a popup within a view controller when a user selects a particular word within a label.
For example
Click here for a link, click here for another link
The label text is pulled from a database so is dynamically added based on the content a user is viewing, i was hoping to use the URL schemes within Swift to show the correct popup depending on what a user selected, but when i add the following within app delegate the popup shows within a separate view controller and not as a pop up view controller.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navVC = storyboard.instantiateViewControllerWithIdentifier("glossaryPopupID") as! UIViewController
self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)
return true
When the popup is close the screen behind is now blank rather than showing the original view controller.
SO my question is, whether this is the best way to achieve this as im not sure its best practice to used URL links within an app to link to other parts of the same app, or is there another way i am missing.
Think i have resolved this now, using the following;
let storyboard = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("test") as! ViewController
let popOverVC = UIStoryboard(name:"Main", bundle: nil).instantiateViewControllerWithIdentifier("sbPopUpID") as! PopUpViewController
storyboard.addChildViewController(popOverVC)
popOverVC.view.frame = storyboard.view.frame
storyboard.view.addSubview(popOverVC.view)
popOverVC.didMoveToParentViewController(storyboard)
self.window?.rootViewController?.presentViewController(storyboard, animated: true, completion: nil)