YogeshChauhan . com

How to create two segues with two UIButtons on a single page (Swift 5.0)?

in Swift on September 7, 2020

Solution


@IBAction func view_1_Cliked(_ sender: UIButton) {
  self.performSegue(withIdentifier: "view_1_Segue", sender: self)
}
    
@IBAction func view_2_Clicked(_ sender: UIButton) {
  self.performSegue(withIdentifier: "view_2_Segue", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if segue.identifier == "view_1_Segue" {
    //add your logic
  }
  else if segue.identifier == "view_2_Segue" {
    //add your logic
  }
  else{}
}

Discussion

prepareForSegue:sender:

It notifies the view controller that a segue is about to be performed.

The default implementation of this method does nothing.

Subclasses override this method and use it to configure the new view controller prior to it being displayed.

The segue object contains information about the transition, including references to both view controllers that are involved.

Because segues can be triggered from multiple sources, you can use the information in the segue and sender parameters to disambiguate between different logical paths in your app.

For example, if the segue originated from a table view, the sender parameter would identify the table view cell that the user tapped. You could then use that information to set the data on the destination view controller.

performSegue(withIdentifier:sender:)

It initiates the segue with the specified identifier from the current view controller’s storyboard file.

Normally, segues are initiated automatically and not using this method.

However, you can use this method in cases where the segue could not be configured in your storyboard file. For example, you might call it from a custom action handler used in response to shake or accelerometer events.

The current view controller must have been loaded from a storyboard. If its storyboard property is nil, perhaps because you allocated and initialized the view controller yourself, this method throws an exception.

prepare(for:sender:)

It notifies the view controller that a segue is about to be performed.

The default implementation of this method does nothing.

Subclasses override this method and use it to configure the new view controller prior to it being displayed.

The segue object contains information about the transition, including references to both view controllers that are involved.

Because segues can be triggered from multiple sources, you can use the information in the segue and sender parameters to disambiguate between different logical paths in your app.

For example, if the segue originated from a table view, the sender parameter would identify the table view cell that the user tapped.

You could then use that information to set the data on the destination view controller.

Sources

amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

How to solve “framework not found” error in Swift?SwiftA Step by Step Guide to Make RSS in XML For Any Website or Blog For FreeMiscWhat is Hoisting in JavaScript?JavaScriptImplicit and Explicit Joins in Oracle SQLSQL/MySQLWhat happened to your Google Analytics Tracking ID, can’t find it?MiscHow to create a simple drop down menu using JavaScript and CSS?CSS