How to use modal view controller with xcode 4.2 frame

I was wondering how to use the storyboard for modular control of the controller. Personally, I prefer to work with xibs, but it seems that the storyboard is gaining popularity and will be a way to go in the future.

The way I usually installed the view controller would be like this: let's say we have ViewControllerA (A for short) and ViewControllerB (for short). Then I would usually put the protocol in Bh, specifying the delegate method when B wants to be fired, and add the id<theProtocol> delegate field as the assign property. Assuming I'm busy with A, and I want to introduce B modally, I would write:

 B* b = [[B alloc] initWithNibName:@"B" bundle:nil]; b.delegate = self; [self presentModalViewController:B animated:YES]; 

Using the storyboard, I know that you can put another view controller in a modal way by ctrl-dragging from a button on the viewcontroller and choosing a modal transition type. I'm just curious, though; where to install the delegate of the new view controller? What is the proper practice of passing things on to your modal view controller? I really don't know what a deal with Segues is ...

+6
source share
2 answers

Take a look at this tutorial.

Accordingly, you should set the delegate as follows:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"AddPlayer"]) { UINavigationController *navigationController = segue.destinationViewController; PlayerDetailsViewController *playerDetailsViewController = [[navigationController viewControllers] objectAtIndex:0]; playerDetailsViewController.delegate = self; } } 

Where @ "AddPlayer" is the name of your modal segment

+5
source

Instead of using the navigation controller, you can directly use the UIStoryboardSegue object passed to prepareForSegue . It has a property called destinationViewController , which is the creator of the view instance. I find that a lot cleaner. This is an example.

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"AddPlayer"]) { PlayerDetailsViewController *playerDetailsViewController = (PlayerDetailsViewController *) segue.destinationViewController; playerDetailsViewController.delegate = self; } } 

IMO I find storyboards to be great because they function as the outline of your application. I also never liked knives. = D

0
source

Source: https://habr.com/ru/post/902322/


All Articles