mluton / EmbeddedSwapping

Demonstration of how to make a custom container view controller manage multiple child view controllers using storyboards.
MIT License
208 stars 30 forks source link

Swift conversion #12

Open NickBols opened 9 years ago

NickBols commented 9 years ago

Hello,

Will you be switching this project to 100% swift code?

I've been away from coding for a while and thought It'd be a good project to try to learn swift so I actually just tried by changing all code to swift and it loads the first viewcontroller however when I click the button to swapviewcontrollers to the second one I always get this really weird error that there is no segue named "secondSegue" present although it clearly is :/

Think it's creating a new instance of containerviewcontroller or something so everything is reset to nil I don't know!

Happy to hear from ya

Nick

NickBols commented 9 years ago

Update: Gotten the 2 tab project to work with some adjustments. I now use a notification to trigger the swapviewcontrollers method in the containerviewcontroller.

    NSNotificationCenter.defaultCenter().postNotificationName("SwapNotification", object: nil)

When I use

    let contVC = ContainerViewController()
    contVC.swapViewControllers()

Similar to what you did in Obj. C then I get an error saying it does not recognize the segue names. Still think this is an instance thing in Swift! Weird language :)

NickBols commented 9 years ago

Ahh! So typically me.. A few minutes after posting a quick fix I discover the real fix :D Now only to add the delegates and more buttons :)

After passing and logging data through 3 vars I've found a way to grab the already loaded instance of the ContainerViewController simply like so:

Main VC

class MainViewController: UIViewController {

var contVC: ContainerViewController?

@IBAction func swapButtonPressed () {
//Execute the swapViewControllers method
    contVC?.swapViewControllers()
}

override func viewDidLoad() {
    super.viewDidLoad()
// Set the reference to the Container View Controller
    contVC = self.childViewControllers[0] as? ContainerViewController
 }
mluton commented 9 years ago

I hadn't really thought about converting this project to Swift but it sounds like a good exercise. Definitely feel free to fork and/or post your own version. :)

NickBols commented 9 years ago

Ok cool I"ll try that haven't forked before on github :) I'm working out some kinks in the delegates and tab ViewControllers not keeping their instance yet but once I've got those I'll def upload it! Code will not be as pretty as your Obj. C version though! Thanks to Swift's (and mine) weirdness :D