Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

push to another view controller without prepareForSegue

How can you push to another view controller without prepareForSegue?

myClassVC *viewController = [myClassVC alloc];
UIStoryboardSegue *segue = [[UIStoryboardSegue alloc] initWithIdentifier:@"pushToMyVC" source:self destination:viewController];

if ([segue.identifier isEqualToString:@"pushToMyVC"]) {
 NSLog(@"logging");
 myClassVC *viewController = (myClassVC *)[segue destinationViewController];
 [self presentViewController:viewController animated:YES completion:nil];        
}
like image 775
Marius Behrens Avatar asked Mar 12 '13 20:03

Marius Behrens


People also ask

How do I show two view controllers in one view controller?

Select the “Show” option from the “Action Segue” menu. Control + click the button and drag to the second ViewController, then select “Show.” Now, on the first ViewController class, you need to override the prepare (for segue) method: 2. Delegate Design Pattern

How do I pass data between view controllers?

Photo by the author. In this article, we’re going to explore five ways to pass data between View Controllers, with Swift code snippets and examples. The five ways are: 1. Segues Segues are a storyboard mode to pass data. Imagine your app has an onboarding screen, and you want to ask the user's name to address them later on another screen:

How do I create a navigationcontroller from a view controller?

Go to the top menu and click on Editor → Embed In → Navigation Controller. This will wrap that current set of segues into a NavigationController, with all of the appropriate changes done for you. There is something that I kept messing up when I was learning this: Make sure when you do this that only ONE View Controller is selected.

What does the “view controller in front” button do?

It just shows the destination View Controller in front of the previous one. This is usually used when the user has to either Finish or Cancel what they are doing, where leaving partway through doesn’t make as much sense. When you set up a new contact in the Contacts app, that is presented modally.


2 Answers

If you want to programmatically invoke a push segue, you give the segue a "storyboard id" in Interface Builder and then you can:

[self performSegueWithIdentifier:"pushToMyVC" sender:self];

Alternatively, if you don't want to perform the segue, you can instantiate the destination view controller and then manually push to that view controller. All you need to do is to make sure that the destination view controller has its own "storyboard id" in Interface Builder, then you can:

UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationController"];
[self.navigationController pushViewController:controller animated:YES];

You said "push" (and hence I used pushViewController above). If you really meant to "present a modal view controller", then that second line is:

[self presentViewController:controller animated:YES completion:nil];

As you can see, you don't have to use prepareForSegue to push to new scene. You only use prepareForSegue if you want to pass information to the destination view controller. Otherwise it is not needed.

Clearly, if you're not using storyboards (e.g., you're using NIBs), then the process is entirely different. But I assume you're not using NIBs because prepareForSegue is not applicable in that environment. But if you were using NIB, it would be as follows:

SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
like image 160
Rob Avatar answered Oct 16 '22 06:10

Rob


[self presentViewController:viewController animated:YES completion:nil]; is not needed, as the segue will push the destination view controller using the transition you selected automatically.

If you don't want to use the segue process you'll need to manually push the view controller with:

[self presentViewController:viewController animated:YES completion:nil];

but make sure you remove the segues in the Storyboard first.

like image 36
Richard Brown Avatar answered Oct 16 '22 07:10

Richard Brown