Yogesh Chauhan's Blog

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


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 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Learn how to give a temporary name to a column or to a table using SQL AliasesSQL/MySQLFor Each Loop in Swift for BeginnersSwiftWhat’s the difference between a Framework and a Library?MiscellaneousWhat happened to your Google Analytics Tracking ID, can’t find it?Miscellaneous5 Steps to Create a Line using Canvas Tag in HTML5HTMLHow to get recent posts in WordPress?WordPress