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
}
}
}
how to programmatically perform segue, or present view, when button pressed in swift3
segue
is Interface Builder lingo. In iOS, you can only display a view controller three ways: push
, present
, show
. Programmatically, you wouldn't call it segue. First, add a target to your button:
registerButton.addTarget(self, action: #selector(registerButtonAction), for: .touchUpInside)
Create the action method:
@objc func registerButtonAction() {
...
}
And if you want to push
to a view controller (using a navigation controller), push to it from inside the action method:
@objc func registerButtonAction() {
let destination = SomeViewController()
navigationController?.pushViewController(destination, animated: true)
}
If you want to present it (modally):
@objc func registerButtonAction() {
let destination = SomeViewController()
destination.transitioningDelegate = SomePresentationAnimationVendor() // for custom presentations
destination.modalPresentationStyle = .custom // for custom
present(destination, 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 do I stop a segue from going through programmatically in Swift?
First , there is no "opposite" to performSegue(withIdentifier: String, sender: Any?).
But the issue is not about this. I think you wired the segue from the login button and gave it an identifier. If you wire a segue from a button directly the button is always going to execute that segue. Doing some operations in the button's action does not effect.
You need to wire a segue from FirstVc to SecondVc (not from the button) and then give the segue an identifier. Then, from the button's action you can check if there is no error and call performSegue(withIdentifier: String, sender:) passing your segue's identifier.
perform segue from programmatically button
- Have you established the segue? In IB ⌃-drag from the the source controller (yellow icon) to the destination view controller)?
- Have you specified the identifier? Select the segue, press ⌥⌘4 and type
moveToNext
in the Identifier field.
Segue to a ViewController from a UIButton in Swift
You need to embed each vc in the tab inside a navigationController , then use this to navigate to a nested vc
@objc func clickStart(_ sender: UIButton) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "VCID") // set id for the destination vc
self.navigationController?.pushViewController(vc,animated:true)
}
And this to navigate to another vc inside the tab
self.tabBarController?.selectedIndex = 1 // 1 is the second tab
How to perform different segue based on condition from a button click in swift?
I think, You are trying to connect the button to perform segue with multiple ViewController Right?? which is not possible
Instead that you have to connect segue between View Controllers
Adding a segue between two viewControllers:
From the Interface Builder, ctrl + drag between the two viewControllers that you want to link. You should see:
And now based on you condition you should perform segue with identifiers like below:
@IBAction func NextViewController(_ sender: UIButton) {
if launchedBefore {
/*Use the Identifier you given in story Board*/
self.performSegue(withIdentifier: "otherVC", sender: self)
} else {
/*Use the Identifier you given in story Board*/
self.performSegue(withIdentifier: "register", sender: self))
UserDefaults.standard.set(true, forKey: "launchedBefore")
}
}
For more Descriptive answer about segue see the answer How to connect two view controllers to one button in storyboard?
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
.
Related Topics
How to Find String B Missing Characters Based on String a and Add Them to String B
How to Update the Constant Height Constraint of a Uiview Programmatically
Why Is an Observedobject Array Not Updated in My Swiftui Application
Go to a New View Using Swiftui
Rounding a Double Value to X Number of Decimal Places in Swift
What Is an Optional Value in Swift
Get Nth Character of a String in Swift Programming Language
Read and Write a String from Text File
Firestore: How to Get Random Documents in a Collection
What Does an Exclamation Mark Mean in the Swift Language
Any Way to Replace Characters on Swift String
Shall We Always Use [Unowned Self] Inside Closure in Swift
Why Can't a Get-Only Property Requirement in a Protocol Be Satisfied by a Property Which Conforms
How to Deal With @Objc Inference Deprecation With #Selector() in Swift 4
Precision String Format Specifier in Swift
How to Create Instances of Managed Object Subclasses in a Nsmanagedobject Swift Extension
Swift Protocol Extension Method Is Called Instead of Method Implemented in Subclass