Presenting a View Controller with a button in a UITableViewCell programmatically (Swift) Presenting a View Controller with a button in a UITableViewCell programmatically (Swift) swift swift

Presenting a View Controller with a button in a UITableViewCell programmatically (Swift)


The presentViewController:animated:completion is an instance method of the UIViewController not UIView or a subclass of. You can try this:

self.window?.rootViewController.presentViewController(specificViewController, animated: true, completion: nil)

However, I suggest that you should use the presentViewController:animated:completion: method from UIViewController. A callback mechanism can be achieved between the UIViewController and the cell.

Like so:Get button click inside UI table view cell


Banning's answer works, however the syntax is old. From Swift 4:

self.window?.rootViewController?.present(vc, animated: true, completion: nil)


swift3 version

let storyboard = UIStoryboard(name: "MainViewController", bundle: nil)let messagesViewController = storyboard.instantiateViewController(withIdentifier: "MessagesViewController") as! MessagesViewControllerself.window?.rootViewController?.present(messagesViewController, animated: true, completion: nil)