Swift: Make button trigger segue to new scene
If you want to have a button trigger the segue transition, the easiest thing to do is Control+Click from the button to the view controller and choose a Segue option (like push). This will wire it up in IB for you.
If you want to write the code to do this yourself manually, you can do so by naming the segue (there's an identifier option which you can set once you've created it - you still need to create the segue in IB before you can do it) and then you can trigger it with this code:
V2
@IBAction func about(sender: AnyObject) {
performSegueWithIdentifier("about", sender: sender)
}
V3
@IBAction func about(_ sender: Any) {
performSegue(withIdentifier: "about", sender: sender)
}
swift: segue on button click
It's very simple.
Follow these steps to create segue from your tableview cell button (click).
- Open your storyboard layout (view controller)
- Add new (destination) view controller.
- Select your button.
- Press & hold control
ctrl
button from keyboard and drag mouse cursor from your button to new (destination) view controller. - Now add following code to your source view controller file (source code)
-
override func performSegue(withIdentifier identifier: String, sender: Any?) {
print("segue - \(identifier)")
if let destinationViewController = segue.destination as? <YourDestinationViewController> {
if let button = sender as? UIButton {
secondViewController.<buttonIndex> = button.tag
// Note: add/define var buttonIndex: Int = 0 in <YourDestinationViewController> and print there in viewDidLoad.
}
}
}
Another way to handle the same.
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?
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 visualize if a button is set to trigger segue to another View Controller?
In Main.storyboard, click on the viewcontroller that you are interested in inspecting. In the top right there is a right arrow that will show you all the segue relationships and outlets
.
Button Activated Segue Not Being Called
I have two segues triggered by buttons
I'm going to suggest that in fact no, you don't. One of the segues is perhaps triggered by a button; that's the one that works as you expect — the segue fires when the button is tapped. But the other segue, though it exists, does not emanate from the other button; it emanates from the cell or from the view controller as a whole.
The way to check this is to select the button and display the Connections Inspector:
If you don't see that — an "action" triggered segue associated with the button — tapping the button won't fire the segue.
Push Action Segue from button to UIViewController
From error logs it looks like you have not embedded UINavigationController
as initial controller in you storyboard.
So first set UINavigationController
as initial controller.
Because push
and pop
operation only performed when there is Navigation stack available.
- Select first ViewController in Storyboard as per screen shot.
- Then Editor -> EmbedIn -> NavigationController
To know how Navigation Stack works, go through this link.
Once you set Navigation as InitialViewController, then set your first viewcontroller you want to show first as rootViewController of that navigation controller.
Then you can drag push from button to secondViewController to push from firstViewController to secondViewController.
As you can read in screenshot that Push(Deprecated)
, so you have to use Show
relationship. Show
also perform same operation as Push
.
Update :
As per your question update, you are facing that you are not able to show other Action Segue. Follow below steps to make it available.
- Select your Navigation Controller in storyboard.
- Enable
Use Trait Variations
option underInterface Builder Document
on right side panel.
For more details check this thread.
Swift prepare for Segue: How to know If a button or a table row initiated Segue
Just pass the index path as sender
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "toMap", sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toMap"
{
let mapViewController = segue.destination as! ViewController
if let buttonItem = sender as? UIBarButtonItem, buttomItem.tag == 1 {
mapViewController.activePlaceRow = -2
}
else if let indexPath = sender as? IndexPath {
mapViewController.activePlaceRow = indexPath.row
}
}
}
Related Topics
API to Capture Live Photos in iOS9
Core Data Taking Time to Insert Records with Fetching Entity & Set as Relationship
Saving Audio After Effect in iOS
Returning Data from Function in Firebase Observer Code Block Swift
Split String into Array of Arrays
Swiftui How Add Custom Modifier with Callback
How to Use " Let Newswiftcolor = Uicolor(Red: 255, Green: 165, Blue: 0, Alpha: 0)
Swift Extension and Enum for Color Schemes
Daily Local Notifications Are Not Working
Peek/Pop Preview Ignores Cell Corner Radius in Collection View
Buttonwithtype' Is Unavailable: Use Object Construction 'Uibutton(Type:)
Receipt Validation on iOS In-App-Purchase Returns Multiple Transaction
How to Use Nsuserdefaults to Store an Array of Custom Classes in Swift
Disable Vertical Scroll in Uiscrollview Swift
Restkit, Coredata and Swift - I Can't Seem to Fetch Results Back Out
How to Check If Device Orientation Is Landscape Left or Right in Swift