DmitrievichR DmitrievichR - 1 year ago 55
Swift Question

how to open viewcontroller in code

How to go to viewcontroller after certain actions, like here:

func requestForAccessToken(authorizationCode: String) {

Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil)
.responseJSON { response in

switch response.result {
case .success(let JSON):
let response = JSON as! NSDictionary
let accessToken = response.object(forKey: "access_token")!
UserDefaults.standard.set(accessToken, forKey: "LIAccessToken")
DispatchQueue.main.async(execute: { () -> Void in
// self.dismiss(animated: true, completion: nil)
let secondViewController: LoginViewController = LoginViewController()
self.present(secondViewController, animated: true, completion: nil)
case .failure(let error):
print("Request failed with error: \(error)")



I create vc in storyboard and make vc class file. Maybe I must make something other.

Answer Source

If you are creatung a viewController in storyboard then you have to instantiate it with storyboard. Try this:

DispatchQueue.main.async(execute: { () -> Void in
    let secondViewController = UIStoryboard(name:"LoginViewController", bundle: nil.instantiateViewControllerWithIdentifier("LoginV‌​iewController") as? LoginViewController
    self.present(secondViewController, animated: true, completion: nil)

Hoping that you have named your storyboard as LoginViewController and provided with storyboardID as LoginViewController