John Doe John Doe - 1 year ago 97
iOS Question

showViewController Not Working

I am developing an app in SWIFT and I am using the following code to show a view controller on a button click.

let storyboard = UIStoryboard(name: "Main", bundle: nil);
var viewName:NSString = "websiteView"
let vc = storyboard.instantiateViewControllerWithIdentifier(viewName) as WebsiteViewController
self.showViewController(vc, sender: self)

it works perfectly when I test it for ios 8 but on ios 7 no matter what I do I get the following error message. I read on a forum that self.showViewController was only available for ios 8 but the compiler doesn't throw an error, does anyone know if I can use it?

showViewController:sender:]: unrecognized selector sent to instance 0x7f9552e7cc50

2014-11-15 09:25:49.565 Throw[80595:613] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Throw.test showViewController:sender:]: unrecognized selector sent to instance 0x7f9552e7cc50'

Answer Source

The compiler didn't throw an error because you have the iOS 8.0 (or 8.1) SDK selected. That method is indeed iOS 8.0+ only. You can choose to use it if you call it conditionally, like so:

if (self.respondsToSelector(Selector("showViewController"))) {
    self.showViewController(vc, sender: self)
} else {
    //An example of the alternate if you are using navigation controller
    self.navigationController?.pushViewController(vc, animated: true)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download