Make Segue programmatically in Swift
You can do it like proposed in this answer:
InstantiateViewControllerWithIdentifier.
Furthermore I am providing you the code from the linked answer rewritten in Swift because the answer in the link was originally written in Objective-C.
let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "identifier") as! SecondViewController
vc.resultsArray = self.resultsArray
EDIT:
Since this answer draws some attention I thought I provide you with another more failsafe way. In the above answer the application will crash if the ViewController
with "identifier" is not of type SecondViewController
. In Swift you can prevent this crash by using optional binding:
guard let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewControllerWithIdentifier("identifier") as? SecondViewController else {
print("Could not instantiate view controller with identifier of type SecondViewController")
return
}
vc.resultsArray = self.resultsArray
self.navigationController?.pushViewController(vc, animated:true)
This way the ViewController
is pushed if it is of type SecondViewController
. If can not be casted to SecondViewController
a message is printed and the application remains on the current ViewController
.
Segue and Button programmatically swift
Create seuge
Assign identifier
and your button target
@IBAction func button_clicked(_ sender: UIButton) {
self.performSegue(withIdentifier: "segueToNext", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueToNext" {
if let destination = segue.destination as? Modo1ViewController {
destination.nomb = nombres // you can pass value to destination view controller
// destination.nomb = arrayNombers[(sender as! UIButton).tag] // Using button Tag
}
}
}
Change segue identifier name programmatically swift
You can't change segue names programmatically. Because a segue defines a transition between two view controllers in your app’s storyboard file and you can rename it in your storyboard.
You can define more than one segues between your view controllers and call segues programmatically like this:
performSegue(withIdentifier: "yourSegueID", sender: nil)
For your problem:
- You can create two segues between your view controllers, and give
two different segue identifier names to them (eg. englishSegue and
arabicSegue). - Create a variable for your segue identifier. (You will use it to
select which of your segue should be triggered.) - Control your language in your view controller (eg. in your
viewDidLoad function or where you should control it)
programmatically and set your language variable as English or Arabic. After that you can trigger it anywhere you want it. For example in a
button click:if (yourIdentifier == "English") {
performSegue(withIdentifier: "englishSegue", sender: nil)
} else if (yourIdentifier == "Arabic") {
performSegue(withIdentifier: "arabicSegue", sender: nil)
}
Perform a segue programmatically
Segues are components of storyboard. If you don't have a storyboard, you can't perform segues. But you can use present view controller like this:
let vc = ViewController() //your view controller
self.present(vc, animated: true, completion: nil)
Creating a segue programmatically without a storyboard
I assume you meant a ViewController
by the word page. So initialize your ViewController
inside your func regSegue(){...}
and present it using present(_:animated:completion:)
method.
Example:
func regSegue(){
let page = PageViewController()
present(page, animated: true, completion: nil)
}
How to add a segue programmatically?
Make sure your tableview delegate is set. If you are using storyboard, make sure delegate outlet in your storyboard is connected properly. If you are creating tableview by code, then you should do tableView.delegate=self;
to set the delegate.
Your code is fine.
And one more thing:
You might need to change this line:
performSegueWithIdentifier("showContent", sender: tableView)
you need to make the sender as the row but not the tableview,so that the prepare for segue will get the sender as row instead of whole tableview.
As you are calling the prepareForSegue overtime you select a row, it makes sense to make the row as sender in performSegueWithIdentifier.
So it would be:
let row=indexPAth.row
performSegueWithIdentifier("showContent", sender: row)
Related Topics
How to Reset/Restart Viewcontroller in Swift
Why Is Forwarding Variadic Parameters Invalid
How to Convert Int to Byte Array of 4 Bytes in Swift
Error: Bool Is Not Convertible to Void:
Why Does an @Objc Enum Have a Different Description Than a Pure Swift Enum
Error When Using Generic as Property Type in Swift
How to Make Protocol Associated Type Require Protocol Inheritance and Not Protocol Adoption
How to Programmatically Change the Alpha of a Uivisualeffectview in a Navigationbar
What Is the Backslash(\) Used for in Swiftui
What's the Difference Between a View and a Viewcontroller
How to Present Different Navigation Title When Large Title Collapse
Swift Convert Currency String to Double
Drawing a 3D Arc and Helix in Scenekit