XCode: Modal windows – Google maps 1


xcode From time to time, you’ll need to show in your iPhone application a view over the entire application without navigating and destroying your workflow of the application. I’ll present the mechanism of modal windows as it is implemented in Cocoa Touch.

Lets take a real case: you have a view in which you want to input an address. Near the address you have a button which will show the user the Map centered on the specific address that he inputted. How this can be implemented?

The answer is quite simple: you need to declare the view from which you launch the modal view as <UINavigationBarDelegate> in the header file (.h for newbies), like this:

@interface CtrlAddEvtView1 : UIViewController <UINavigationBarDelegate>

This way, the view will know “magically” how to interact with children views.

Next we need to create the IBAction (the exposed action for linking to a button action in Interface Builder) in the header file :

- (IBAction) showMap:(id)sender;

Now we’ll move into the implementation file (corresponding .m file) and use the following code for calling our new view:

-(IBAction)showMap:(id)sender{
    // create the modal view
    CtrlMapView *modalViewController = [[CtrlMapView alloc] initWithNibName:@"MapView" bundle:nil];

    // present info page as a modal view
    [[self navigationController] presentModalViewController:modalViewController animated:YES];   
}  

Next thing that needs to be done is to link a button from the Interface Builder on the view from which you want to launch the new one and link it to the showMap action.

To return from the newly opened modal view we will need to create another action in its header file:

- (IBAction)btnCancelClick:(id) sender;

The implementation file will of course hold the implementation of the above method:

- (IBAction)btnCancelClick:(id) sender{
    [[self parentViewController] dismissModalViewControllerAnimated:TRUE];
}

 

And that’s it, folks! Enjoy!

Leave a comment

Your email address will not be published. Required fields are marked *