So, you could create viewControllers with their controls and load the views in the IBAction as needed.
Their class is FirstViewController and SecondViewController respectively
From the tabBar buttons you would call first or secondController:
class SwitchingViewController: UITabBarController {
private var firstViewController: FirstViewController!
private var secondViewController: SecondViewController!
override func viewDidLoad() {
super.viewDidLoad()
firstViewController = storyboard?.instantiateViewControllerWithIdentifier("First") as! FirstViewController
firstViewController.view.frame = view.frame
switchViewController(from: nil, to: firstViewController)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
if firstViewController != nil && firstViewController!.view.superview == nil {
firstViewController = nil
}
if secondViewController != nil && secondViewController!.view.superview == nil {
secondViewController = nil
}
}
@IBAction func changeSelection(sender: UISegmentedControl) {
if secondViewController?.view.superview == nil {
if secondViewController == nil {
secondViewController = storyboard?.instantiateViewControllerWithIdentifier("Second")
as! SecondViewController
}
} else if firstViewController?.view.superview == nil {
if firstViewController == nil {
firstViewController = storyboard?.instantiateViewControllerWithIdentifier("First")
as! FirstViewController
}
let selection = sender.selectedSegmentIndex
switch selection {
case 0: switchViewController(from: firstViewController, to: secondViewController)
case 1: switchViewController(from: secondViewController, to: firstViewController)
default: break
}
}
private func switchViewController(from fromVC:UIViewController?, to toVC:UIViewController?) {
if fromVC != nil {
fromVC!.willMoveToParentViewController(nil)
fromVC!.view.removeFromSuperview()
fromVC!.removeFromParentViewController()
}
if toVC != nil {
self.addChildViewController(toVC!)
self.view.insertSubview(toVC!.view, atIndex: 0)
toVC!.didMoveToParentViewController(self)
}
}
}
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func firstButtonPressed(sender: UIButton) { // A button in the view
let alert = UIAlertController(title: "Firs Button",
message: "pressed on the first view",
preferredStyle: .Alert)
let action = UIAlertAction(title: "Done", style: .Default, handler: nil)
alert.addAction(action)
presentViewController(alert, animated: true, completion: nil)
}
}
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func secondButtonPressed(sender: UIButton) { // A different button in second view
let alert = UIAlertController(title: "Second Button",
message: "pressed on the second view",
preferredStyle: .Alert)
let action = UIAlertAction(title: "Done", style: .Default, handler: nil)
alert.addAction(action)
presentViewController(alert, animated: true, completion: nil)
}
}