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 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #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

#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 #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS? #Jan 8 How to Install Xcode Command Line Tools on MacOS?
You might also like these
List of WordPress directories functionsWordPressHow to Recognize an Array in JavaScript?JavaScriptWordPress: How to find all posts from a custom post type with specific custom field?WordPressHow to create ‘share on LinkedIn’ link using just HTML?HTMLHow to define variables in SCSS (Sass)?SCSSKilling A Project Part 2: Who should make the decision to kill a project?Miscellaneous