ThisIsJustAHobby
ThisIsJustAHobby

Reputation: 69

How to create a segue to a Table View Controller Scene directly in a swift class file?

I am implementing FB Login. I have instantiated the FBSDKLoginButton() and added it to view directly from my ViewController.swift file (created and presented through the source code).

Normally I present a view modally by dragging and connecting the button to the new view in Interface Builder. However this button does not exist in my storyboard. How can I complete this task, but directly from my ViewController.swift file.

Upvotes: 1

Views: 380

Answers (2)

Amit
Amit

Reputation: 4896

You can add manual segues from one ViewController to other ViewController.

In your FirstViewController in storyboard :

enter image description here

Connect the segue.

Then select the connected segue and add Identifier:

enter image description here

Now you can use:

self.navigationController?.performSegue(withIdentifier: <The Identifier name>, sender: <If you want to send some data with it>)

you can use prepare(for segue: UIStoryboardSegue, sender: Any?):

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

}

to send data to the SecondViewCntroller.

Hope this helps.

Upvotes: 3

Ankit Jayaswal
Ankit Jayaswal

Reputation: 5689

Direct segue needs a connection from UI object to other view-controller. Rather segue, you can use pushViewController:

enter image description here

let sb = UIStoryboard(name: "Main", bundle: Bundle.main)
if let viewcontroller = sb.instantiateViewController(withIdentifier: "storyboardIdentifier") as? YourViewController {
    self.navigationController?.pushViewController(viewcontroller, animated: true)
}

Upvotes: 1

Related Questions