Assumed there is a 'ViewControllerOne' in Storyboard 'A'. If a user tapped a button on it, 'ViewControllerOne' will call Storyboard 'B' and show it as a popover on the screen. It seems that I can't find any references for my problem.
I would like to call another Storyboard from a Storyboard and show it as a popover, is it possible to do this? Thank you.
EDIT
I've tried the solution provided by #Sohil R. Memon, but I got an error :
reason: 'Storyboard (<UIStoryboard: 0x7fe69a841f00>) doesn't contain a view controller with identifier 'PaymentOnly''
I've added the RestorationID to the View Controller.
But if I change the code to :
let storyBoard : UIStoryboard = UIStoryboard(name: "PaymentOnly", bundle:nil)
let navigationController: UINavigationController = storyBoard.instantiateInitialViewController() as! UINavigationController
let viewController = navigationController.viewControllers[0] as! PaymentOnlyViewController
self.navigationController?.pushViewController(viewController, animated: true)
It worked but doesn't pop over the view. Below is the code from #Sohil R. Memon :
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "PaymentOption" {
let storyBoard = UIStoryboard(name: "PaymentOnly", bundle: nil)
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("PaymentOnly") as! PaymentOnlyViewController
self.presentViewController(vc, animated: false, completion: nil)
}
}
You can obviously present / popover any of the UIViewController from anywhere in any UIStoryBoard.
Expecting this is your first UIViewController and you want to call any other UIViewController from other UIStoryBoard. So for that find the below code:
let storyBoard = UIStoryboard(name: "storyboard_name", bundle: nil)
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("chatVC") as! ChatVC
self.presentViewController(vc, animated: false, completion: nil)
This way you can present or push any of the UIViewController.
Popover reference link.
Related
I created a view in .xib. On it, I placed a button on which I want to open another ViewController. I assigned a storyboardID to this ViewController. But I can't push the new controller, how can this problem be solved?
My code:
class MyView: UIView {
...
#IBAction func buttonPressed(_ sender: Any) {
let storyboard = UIStoryboard.init(name: "ViewController", bundle: Bundle.init())
let vc = storyboard.instantiateViewController(withIdentifier: "VC") as! ViewController
let navigationController = UINavigationController.init()
navigationController.pushViewController(vc, animated: true)
}
...
}
class ViewController: UIViewController {
...
}
Xcode gives this error:
'Could not find a storyboard named' ViewController'
but I checked there are no errors. I reloaded Xcode - no result.
Seems you have more than one issue.
You are trying to push a UIViewController into a non presented UINavigationController
You are creating an instance from a UIStoryboard with wrong identifier
First you need to choose the correct UIStoryboard which I am assuming it's main, then you have to present the UINavigationController from the holder UIViewController or trying to push from that UIViewController if it's has a UINavigationController,
Part 1: Fixing the instances,
let storyboard = UIStoryboard.init(name: "main", bundle: Bundle())
let vc = storyboard.instantiateViewController(withIdentifier: "VC") as! ViewController
let navigationController = UINavigationController(rootViewController: vc)
Now you have a navigationController that holds a your VC
Part 2: You want to present this navigation from your holding VC you can either add a listener to the UIButton from that VC and present/push from there
so lets wrap this up create a function in your VC that holds the UIView
#objc func pushToMyVc() {
let storyboard = UIStoryboard.init(name: "main", bundle: Bundle())
let vc = storyboard.instantiateViewController(withIdentifier: "VC") as! ViewController
let navigationController = UINavigationController(rootViewController: vc)
}
And in your UIView add this variable var selector: (() -> Void)?
Now in #IBAction func buttonPressed call this function selector?()
Now all we need is to pass what function I want to use when this button gets clicked in our case the function pushToMyVC that we put in our main UIViewController so in our didLoad or wherever you're calling this custom view
set the selector like this
myView.selector = pushtoMyVC
Side Note: you can't present/push UIViewController from non
UIViewController classes
Try changing how you fetch the storyboard from
let storyboard = UIStoryboard.init(name: "ViewController", bundle: Bundle.init())
to
let storyboard = UIStoryboard(name: "ViewController", bundle: nil)
note that the storyboard ID needs to be exactly ViewController.
If this still shows the same error, I'd say you have an issue with the storyboard target membership, so check that in the file inspector on the right side of the Xcode editor.
You should check your Storyboard file name again, because when you want to initiate a UIViewController from a Storyboard file, the name parameter in UIStoryboard initiation has to be exactly the same as the Storyboard file name.
#IBAction func buttonPressed(_ sender: Any) {
// Let's say that you have a Storyboard file named ViewController.storyboard and a UIViewController class named ViewController.swift
let destinationVC = UIStoryboard(name: "ViewController", bundle: nil).instantiateViewController(withIdentifier: "VC") as! ViewController
self.navigationController?.pushViewController(destinationVC, animated: true)
}
Hope this helps your issue
you should take a delegate back to viewController that contains current view, and call navigationController.push from there
More specific,
Let's say I have a UIViewController A, when I press a button in A that is created by code, I would like to display another UIViewController B, where B is designed in the storyboard. Can it be done by coding, but not using the connections between view controllers in the storyboard. Thanks.
Absolutely,
Make sure you have added the storyBoardIdentifier to ViewController B.
Finally, In your ViewController A :
let YourStoryBoard = UIStoryboard(name: "YourStoryBoard", bundle: nil)
let vc = YourStoryBoard.instantiateViewController(withIdentifier: "VCB_StoryBoard_Identifier") as! ViewControllerB
If your ViewController has a UINavigationController embedded to it then,
self.navigationController?.pushViewController(vc, animated: true)
Else present ViewController B from ViewController A
self.present(vc, animated: true, completion: nil)
EDIT:
Set your storyboard identifier as shown in image below
Associate your ViewControllerB class with storyboard VC by setting the ViewController's class. As shown below.
You have to make a new cocoa touch class -> UIViewController -> "ViewContollerB"
Add the name in the storyboard to the view and than you can do:
let storyboard = UIStoryboard(name: "YourStoryBoard", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "VCB_StoryBoard_Identifier") as! ViewContollerB
Try this:
On button tap in UIViewController A,
func onTapButton()
{
let controller = UIStoryboard.init(name: "YOUR_STORYBOARD_NAME", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerBIdentifier") as! ViewControllerB
self.present(controller, animated: true, completion: nil)
}
Here,
ViewControllerBIdentifier : StoryboardID corresponding to UIViewController B
ViewControllerB : class corresponding to UIViewController B
I am attempting to transfer a value between my view controllers. When I simply connect my button to the other view controller, like this:
I am able to make it work. I am overriding prepareforSegue.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestinationViewController : ViewTwo = segue.destinationViewController as! ViewTwo
DestinationViewController.scoretext = score.text!
}
It all works fine but when I open the other controller programatically, the value isn't transferred. This is the code I'm using for that...
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc : ViewTwo = storyboard.instantiateViewControllerWithIdentifier("viewtwo") as! ViewTwo
self.presentViewController(vc, animated: true, completion: nil)
When you use instantiateViewControllerWithIdentifier, prepareForSegue is not called. You will have to assign scoretext after instantiation only.
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc : ViewTwo = storyboard.instantiateViewControllerWithIdentifier("viewtwo") as! ViewTwo
vc.scoretext = score.text!
self.presentViewController(vc, animated: true, completion: nil)
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)
I have the following ViewController structure, see image below.
Whenever I want to move from ViewController 1 to any of the other main controllers I use self.tabBarController?.selectedIndex = indexNumber
// for instance, this would take me to ViewController 3
self.tabBarController?.selectedIndex = 2
Based on the picture below, how can I go from ViewController 1 to TargetViewController programmatically when a button is tapped in ViewController 1?
FYI -
In the picture below I'm showing a button in ViewController 3 this is just to show the storyboard structure, the actual button-tap will happen in ViewController 1
EDIT:
Here is how you do it based on Prashant Tukadiya's answer.
ViewController 1
In ViewController 1 add the following in your tap event.
self.tabBarController?.selectedIndex = 2
let nav = (self.tabBarController?.viewControllers?[2] as? UINavigationController)
let vc = TargetViewController.viewController()
nav?.pushViewController(vc, animated: true)
TargetViewController
In your TargetViewController add the following class method.
class func viewController() -> TargetViewController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
return storyboard.instantiateViewController(withIdentifier: "targetViewControllerID") as! TargetViewController
}
Add targetViewControllerID in the Storyboard ID field.
I was facing same before a couple of days and finally, I got a solution maybe this will help you.
TabController.shared.tabcontroller.selectedIndex = 1
let navigation = TabController.shared.tabcontroller.selectedViewController?.navigationController
let SavedAddress = self.storyboard?.instantiateViewController(withIdentifier: "SavedAddressVC") as! SavedAddressVC
navigation?.pushViewController(SavedAddress, animated: true)
I have used the same solution. and this is the working code for me.
You can directly give identifier to the TargetViewController from storyboard and load from storyboard then and push or present it.
like add this method in your TargetViewController
class func viewController () -> TargetViewController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
return storyboard.instantiateViewController(withIdentifier: "yourIdentifer") as! TargetViewController
}
and on tap event
let vc = TargetViewController.viewController()
self.navigationController?.pushViewController(vc, animated: true)
EDIT
After reading comments got clear idea about your requirements
On button action from ViewController1, You want to goto TargetViewController but when press back you want back to viewController 3
Select particular index first
self.tabBarController?.selectedIndex = 2
After that you need to grab UINavigationController
let nav = (self.tabBarController?.viewControllers?[2] as? UINavigationController)
then Push ViewController
let vc = TargetViewController.viewController()
nav?.pushViewController(vc, animated: true)
Note: Don't forgot add identifier to storyboard to TargetViewController and also add class func viewController () -> TargetViewController method to TargetViewController
Hope it is helpful
In action function of a button in ViewController 1 , you can use this :
func presentMethod(storyBoardName: String, storyBoardID: String) {
let storyBoard: UIStoryboard = UIStoryboard(name: storyBoardName, bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: storyBoardID)
self.definesPresentationContext = true
self.present(newViewController, animated: true, completion: nil)
}
//usage
presentMethod(storyBoardName: "Main", storyBoardID: "TargetViewController")