1
votes

I have an existing segue linked to a button. I would like the button and segue to remain but need a way to trigger the segue programmatically in my view did load after a successful IF statement. The segue was created using storyboard.

I tried performing performSegueWithIdentifier but there is no "self sender" because there is no UI being triggered.

[self performSegueWithIdentifier:@"segueIdentifier" sender:self];

Is there a simple way to do this? No phantom buttons or forcing the existing button to execute.

1
The code you have shown is correct. You probably want to do it in viewWillAppear or viewDidAppear rather than viewDidLoad - Paulw11
GREAT! that was the problem. I was putting it in the viewDidLoad. - Johnny Caraveo
small question, if i want to performsegue entirely from code and not from storyboard, how can I do it? Secondly, if I delete the segue from Storyboard the app crashes when i do the [self performSegueWithIdentifier:@"segue" sender:self]; - Mohsin Khubaib Ahmed

1 Answers

0
votes

A segue does not require a user interaction to be triggered. The sender is the object that the prepareForSegue method will receive.

Using self is actually the correct way because it means the view controller triggered the segue and not a view object.

If you have no use for the sender in prepareForSegue then keep the call as-is and ignore it in the implementation.

Also, this call will not work in viewDidLoad since the view in still not the in windows hierarchy. You should wait until after viewDidAppear before performing any segues.