1

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")
                UserDefaults.standard.synchronize()
                ///here...
                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.

4
  • You have to create the segue in Interface Builder. Commented Sep 20, 2016 at 14:33
  • Did you try this? let secondViewController = UIStoryboard(name:"LoginViewController", bundle: Bundle.main).instantiateViewControllerWithIdentifier("LoginViewController") as? LoginViewController Commented Sep 20, 2016 at 14:36
  • @Santosh no, m trying now. I have one more question i must set identifier in storyboard id ? and after make self.present(secondViewController, animated: true, completion: nil) Commented Sep 20, 2016 at 14:50
  • Yes, see my answer Commented Sep 20, 2016 at 14:51

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

i get error on breakpoint with let secondViewController = UIStoryboard(name:"LoginViewController", bundle: nil.instantiateViewControllerWithIdentifier("LoginV‌​iewController") as? LoginViewController
15 libdyld.dylib 0x000000010510068d start + 1 16 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException
Glad, it helped you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.