Understanding Performseguewithidentifier

Understanding performSegueWithIdentifier

First, you have to have set up the segue in your storyboard and give it the appropriate identifier. (Click on the segue (left panel) and then click on Attributes (right panel).

You can then link this to buttons or selection of table rows from your storyboard, or you can call it in code using performSegueWithIdentifier:sender:.

After this, your view controller will be sent the prepareForSegue:sender: message. You override this method in your view controller subclass, and can configure the target view controller as follows:

TargetViewController *targetVC = (TargetViewController*)segue.destinationViewController;
targetVC.string1 = string1;

And so forth. The sender in this method will be the object that you use as the sender in the original method call.

Understanding performSegueWithIdentifier

First, you have to have set up the segue in your storyboard and give it the appropriate identifier. (Click on the segue (left panel) and then click on Attributes (right panel).

You can then link this to buttons or selection of table rows from your storyboard, or you can call it in code using performSegueWithIdentifier:sender:.

After this, your view controller will be sent the prepareForSegue:sender: message. You override this method in your view controller subclass, and can configure the target view controller as follows:

TargetViewController *targetVC = (TargetViewController*)segue.destinationViewController;
targetVC.string1 = string1;

And so forth. The sender in this method will be the object that you use as the sender in the original method call.

How do I use performSegueWithIdentifier: sender:?

To make a kind of 'splash screen' for your app, just create the view for it in your Storyboard and set it as the entry point (or root of a Navigation Controller etc). Create a segue like you have previously, except drag a segue from the 'Splash' view controller, to the 'Main Menu' controller. With the segue selected, set its Identifier in the Attributes inspector to ShowMainMenu.

Create a method in the 'Splash' view controller that performs the segue:

- (void)showMainMenu {
[self performSegueWithIdentifier:@"ShowMainMenu" sender:self];
}

In the 'Splash' view controller's viewDiDLoad method, add:

[self performSelector:@selector(showMainMenu) withObject:nil afterDelay:5.0];

There you have it!

swift performSegueWithIdentifier sender value

As @iosDev82 says in his answer, sender is an optional that names the object (if any) that triggered the segue.

If you trigger a segue through code in a view controller, you could pass the view controller (self), or you could pass nil. It's just a piece of information that is passed along to prepareForSegue (again as iOSDv82 says.)

If you trigger a segue in the code of an IBAction method, your IBAction may have it's own sender parameter (frequently a button.) In that case you can pass along the sender parameter to the performSegueWithIdentifier method.

Example:

@IBAction func buttonAction(sender: UIButton)
{
//In this case the button IBAction takes a pointer to the button as a param.
//Pass it on to the segue in case performWithSegue needs it.
self.performSegueWithIdentifier("someID", sender: sender)
}

PerformSegueWithIdentifier blowing up

You must add a segue in your storyboard for SecondViewController to SignupViewController and set that segue identifier to "SignupViewController". Then it will work.

I guess now the segue is available only from Main Table View Controller to SignupViewController with identifier "SignupViewController", if so it will work only from Main Table View Controller.

How to call performSegueWithIdentifier from xib?

The problem is that your UIView subclass is calling viewController().goToSecond(). That's not doing what you think it is. The viewController() isn't referencing the view controller that loaded your custom view. It's instantiating a second, orphaned instance of that class (not connected to any storyboard) and therefore cannot find the segue.

If you're really going to have this custom UIView subclass initiate a segue, you need to pass a reference to your original view controller to the custom view. So add a property to the custom view subclass that can hold the reference to its view controller, and when the view controller instantiates this custom view, it has to set that property.


For example:

import UIKit

protocol CustomViewDelegate: class { // make this class protocol so you can create `weak` reference
func goToNextScene()
}

class CustomView: UIView {

weak var delegate: CustomViewDelegate? // make this `weak` to avoid strong reference cycle b/w view controller and its views

@IBAction func toSecondButton(sender: AnyObject) {
delegate?.goToNextScene()
}

}

And then

import UIKit

class ViewController: UIViewController, CustomViewDelegate {

override func viewDidLoad() {
super.viewDidLoad()

let myCustomView = NSBundle.mainBundle().loadNibNamed("customView", owner: self, options: nil)[0] as! CustomView
myCustomView.delegate = self

// ... do whatever else you want with this custom view, adding it to your view hierarchy
}


func goToNextScene() {
performSegueWithIdentifier("toSecond", sender: self)
}

...

}

performSegueWithIdentifier is being skipped *SWIFT

You should move your code into the viewWillAppear or viewDidAppear method.
Then it should use properly.

But now the view flickers a short time. To remove that flickering, just hide the view in your viewWillApear method:

self.view.hidden = true

Also the viewDidLoad method doesn't get called everytime the view appears but only the first time it loads. If you for example perform a segue and return back to the view, it doesn't load again but only calls viewWillAppear and viewDidAppear again.



Related Topics



Leave a reply



Submit