I want to embed viewcontroller in containerview in code.
I put a containerview on storyboard and I want to embed a viewcontoller on another storyboard.
I tried below but it doesn't work.
Do you have any suggestions?
let storyboard: UIStoryboard = UIStoryboard(name: "AnotherStoryboard", bundle: nil)
let vc:OtherViewController = storyboard.instantiateViewControllerWithIdentifier("OtherViewController") as OtherViewController
self.addChildViewController(vc)
containerView.addSubview(vc.view)
vc.didMoveToParentViewController(self)
You have to do two changes for make your code working
Change containerview to self.view
Add OtherViewController as storyBoard ID for OtherViewController.
This will make your code work.
Related
I have a TabBarController with two tabs Cases & Settings
I would like to take the user to CaseSummaryTVC which is nested like this
TabBarController > Cases (NavigationController, Storyboard Id = 'tvcNav' ) > CasesTVC (TableViewController) > CaseSummaryTVC (TableViewController, Storyboard Id = 'CaseSummaryTVC').
I am using the below code in AppDelegate, which takes me to the 'CaseSummaryTVC' but doesn't show the TabBar on the bottom.
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navController = mainStoryboard.instantiateViewController(withIdentifier: "tvcNav") as! UINavigationController
let caseSummaryTVC = mainStoryboard.instantiateViewController(withIdentifier: "CaseSummaryTVC") as! CaseSummaryTVC
navController.pushViewController(caseSummaryTVC, animated: true)
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
It looks like you're setting your window's rootViewController to be 'navController', which doesn't seem to be wrapped in a UITabBarController.
I'm not sure what your storyboard looks like, but it sounds like you have the view hierarchy setup correctly there.
You could do one of the following:
1) Remove the programmatic view controller instantiation and have the storyboard take care of setting the rootViewController to the UITabBarController.
2) Instantiate the UITabBarController programmatically like you have done here with the other view controllers, and make sure it's set up to have the navController as one of it's tabs.
Here are my storyBoards
Tabbar Storyboar
other storyboard
here is my code to Move TabbarStoryBoard to Other Storyboard
let storyboard = UIStoryboard(name: "Tabbar", bundle: Bundle.main)
let vc = storyboard.instantiateViewController(withIdentifier: "LoginController") as? LoginController
self.navigationController?.pushViewController(vc!, animated: true)
Now when I move from Tabbar StoryBoard from Other storyboard's LoginController, and signIn with email & password and want to redirect user to Tabbar storyboard is not pushing. It is successfully pushing from Tabbar storyboard to other storyboard's view controller, even it is not pushing in same storyboard to other viewcontroller
It is not giving any error, and also not pushing to Tabbar Storyboard.
What is my issue?
Note : Tabbar Storyboard's TabbarViewcontroller is my initial viewController
Story Board Referencing is the best thing for this... Hope these links will work.
Use Storyboard References While Retaining Icons & Text for Tab Bar Controller
How to add storyboard reference in another storyboard
In my app I use side bar as in facebook. when the user slides out the side bar a uiimageview is displayed. when user taps on the image it takes hm to a different viewcontroller. the problem i am facing is that I have created sidebar programatically and the other view to which I want to navigate the user is created using storyboard. So my source view is created programatically and destination view is created using storyboard. So can someone explain me if there is any way of using "Segue" in this scenario. Since i can not create segue using storyboard I need to do it programatically but even after a lot of googling i could not find the answer.
Well, to get another instance of another storyboard programmatically you can use something like:
let newController = UIStoryboard(name: "MyStoryboard", bundle: nil).instantiateViewControllerWithIdentifier("MyIdentifier") as! MyViewController
and then you push to your navigation controller, or add as a child view controller or something...
If you don't wanna bother with identifiers you can just use the instantiateInitialViewController instead of instantiateViewControllerWithIdentifier
Might help
"userSB" is the viewcontroller storyboard identifier
#IBAction func tapSearchCriteria(_ sender: Any?) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let aVC = storyboard.instantiateViewController(withIdentifier: "userSB") as? AViewController
aVC?.modalPresentationStyle = UIModalPresentationStyle.custom
aVC?.transitioningDelegate = self
aVC?.udelegate = self
self.present(aVC!, animated: true, completion: nil)
}
I create a view controller vc in storyboard and place a View1 on it and add all constraint to it. Now I want to add a vc as a subview. I use this code :
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("LeftMenuListIdentifier")
self.view.addSubview(vc.view)
vc controller's add as subview to my current controller but it does not show any subview of vc controller'view means View1.
If I push vc controller to my current view controller then it shows View1.
self.navigationController?.pushViewController(vc, animated: true)
Please suggest what should I do . I want to add a view controller from storyboard as a subview.
Kindly Follow below steps for resolve your issue.
First Create UIViewController object.
after that Add object as a childviewcontroller as Like ( self.addChildViewController("Your Controller Object"))
After that add controller on self.view as add subview.( `self.view.addSubview("Your Controller Object"))
i think it will working
you don't need to push your vc, just add it to current viewcontroller using addChildViewController(vc);, this link may help you
I have 2 view controller ,and I disabled the initial view controller of first view controller ,and enabled the second view controller,but when start the project,the initial view controller is still the first view controller ,what should I do? Thanks!
Tap the second view controller, and select "Is initial View Controller" in Attributes inspector.
Alternatively you can do this with code too.
In your AppDelegate class's didFinishLaunchingWithOptions method you can write
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let secondVC = storyBoard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
self.window?.rootViewController = secondVC
Edit Swift 3.0
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let secondVC = storyBoard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.window?.rootViewController = secondVC
Assuming that your storyboard name is Main.Storyboard and your SecondViewController Storyboard ID and Restoration ID is also set in Identity Inspector and use Storyboard ID is checked.
If you are starter like me ( both iOS and OSX mechine ) and you wanted to change the entry point within same storyboard, then,
-> long press on the entry point arrow.
-> drag it to the view controller you wish to make the starting point, the arrow will now move to your new screen.