Swift 3 - Loading Multiple Viewcontrollers at Launch

Swift 3 - loading multiple ViewControllers at launch

Add in your main view controller

var secondViewController:UIViewController!

And in your viewDidLoad:

secondViewController: UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourIdentifier") as! SecondViewController

That's it. When you want to present it, use:

self.present(secondViewController, animated: true, completion: nil)

Load and Present a second viewcontroller at launch

You need to do it at the proper place in your view controller. There are several methods that gets called when your view controller is presented, and they are meant for different tasks.

To present another view controller you should put it in viewWillAppear: or viewDidAppear:, as viewDidLoad: is too early in the presentation.

Read more about these methods here:

https://stackoverflow.com/a/5659007/4543629

Wait to load child views in UITabBarController?

You could start with an empty tab bar controller and add the child view controllers programmatically as the API call completes.

However, this begs the question, why show the tab bar controller at all? Instead, I'd suggest that you start with an empty 'loading' screen and segue to the tab bar controller when the API call completes.

I'd also suggest that, rather than subclassing TabBarController for your API calls, you create a separate manager object to perform the update process, potentially as a singleton object (depending on other code considerations). You can then either call the shared instance from your view controllers, or inject the API call manager directly into each instance in prepare(for segue: sender:) on the presenting / parent view controller.

Pass Multiple ViewControllers through One Container View - Xcode 8, Swift 3

Figured it out!!
I removed the blue Container View and swapped it for a Scroll View
Here's the Code for anyone else who is interested in this. Still working on making toe Container View's height dynamic to the content.

This tutorial helped a lot: Tutorial

Main View Controller:

var container: ContainerViewController!

override func viewDidLoad() {
container!.segueIdentifierReceivedFromParent("first")

}

@IBAction func firstBtnPressed(_ sender: Any) {
let vc = "first"
container!.segueIdentifierReceivedFromParent(vc)

}

@IBAction func secondBtnPressed(_ sender: Any) {
container!.segueIdentifierReceivedFromParent("second")
}

@IBAction func thirdBtnPressed(_ sender: Any) {
container!.segueIdentifierReceivedFromParent("third")
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "container"{

container = segue.destination as! ContainerViewController

}
}

Container View(Purple View)

open class ContainerViewController: UIViewController {
//Manipulating container views
fileprivate weak var viewController : UIViewController!
//Keeping track of containerViews
fileprivate var containerViewObjects = Dictionary<String,UIViewController>()

/** Specifies which ever container view is on the front */
open var currentViewController : UIViewController{
get {
return self.viewController
}
}

fileprivate var segueIdentifier : String!

/*Identifier For First Container SubView*/
@IBInspectable internal var firstLinkedSubView : String!

override open func viewDidLoad() {
super.viewDidLoad()
}

open override func viewDidAppear(_ animated: Bool) {
if let identifier = firstLinkedSubView{
segueIdentifierReceivedFromParent(identifier)
}
}

override open func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func segueIdentifierReceivedFromParent(_ identifier: String) {
self.segueIdentifier = identifier
self.performSegue(withIdentifier: self.segueIdentifier, sender: nil)
}

override open func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == segueIdentifier {
//Remove Container View
if viewController != nil {
viewController.view.removeFromSuperview()
viewController = nil
}
//Add to dictionary if isn't already there
if ((self.containerViewObjects[self.segueIdentifier] == nil)) {
viewController = segue.destination
self.containerViewObjects[self.segueIdentifier] = viewController
} else {
for (key, value) in self.containerViewObjects {
if key == self.segueIdentifier {
viewController = value
}
}
}

self.addChildViewController(viewController)
viewController.view.frame = CGRect(x: 0,y: 0, width: self.view.frame.width,height: self.view.frame.height)
self.view.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)
}
}
}

How to push two view controllers but animate transition only for the second one?

The solution you're looking for if you're in the firstVC:

NSMutableArray *controllers = [self.navigationController.viewControllers mutableCopy];
[controllers addObject:secondVc];
[controllers addObject:thirdVC];
[self.navigationController setViewControllers:controllers animated:YES];

This will animate in the thirdVC without the secondVc becoming visible in the process. When the user press the back button, they will return to the secondVc

Changing view controllers Swift 3

Have you embed you storyboard to navigation controller.In your code navigation controller optional so controller is not able get navigation controller.

you have to embed your first storyboard to navigation controller And it will work.You can do it by In Xcode>Editor>Embed In>Navigation Controller.

how to load child view controllers or container views - Swift

As i understand there are 2 container views and you want to see the blue view when the segmented is first and you want to see the green view when the segmented is second if it is true the solution is like this;

You should init the uivew(hold on the ctrl and drag it to viewcontroller for both blue and green container view)(you need to add 2 different container view) and write

if segmented.selectedIndex == 0 { 

greenView.isHidden = true
blueView.isHidden = false

} else if segmented.selectedIndex == 1 {

greenView.isHidden = false
blueView.isHidden = true
}

How do I show two view controllers in a single view?

Container Views should help you with your app.

https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html

"Container view controllers are a way to combine the content from multiple view controllers into a single user interface."



Related Topics



Leave a reply



Submit