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

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
How to solve “framework not found” error in Swift?SwiftUnderstand Inheritance in PHP in this Basic Example (For Beginners)PHPWhat are keys in React?ReactWordPress plugin development: How to fix a SQL injection?WordPressMoving folders in WordPress using codeWordPressHow to make flexbox items of the same size?CSS