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 add Read More Read Less Button using JavaScript? #2 How to check if radio button is checked or not using JavaScript? #3 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
URL paths in DrupalDrupalExplanation of PostgreSQL PgAdmin interfacePostgresHow to reprocess failed items in Etrieve Flow?MiscellaneousDebugging in WordPress Part 2: WP_DEBUG_LOG and WP_DEBUG_DISPLAYWordPressTwo ways we can use colon(:) in Envision BasicEnvision BasicWindow setInterval() Method in JavaScriptJavaScript