MOBILE

iPad SDK : Popovers - The Font Name Popover (part 2)

7/14/2011 3:52:36 PM

2. The Back End

Now it's time to implement the portions of DudelViewController that will fire up the FontListController, dismiss its popover when the user makes a selection, and grab the selected value. Start with an import:

#import "FontListController.h"

Then fill in this previously empty method:

- (IBAction)popoverFontName:(id)sender {
FontListController *flc = [[[FontListController alloc]
initWithStyle:UITableViewStylePlain] autorelease];
flc.selectedFontName = self.font.fontName;
[self setupNewPopoverControllerForViewController:flc];
flc.container = self.currentPopover;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(fontListControllerDidSelect:)
name:FontListControllerDidSelect
object:flc];
[self.currentPopover presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}

This method creates and configures a FontListController instance. Part of the configuration involves calling a method named setupNewPopoverControllerForViewController: (which we're going to create in just a minute). It also sets us up as an observer for a notification, which will tell us that the user selected something, and then displays a popover.

What's not really clear here is the final line, which contains self.currentPopover. We haven't set that, have we? Well, the following auxiliary method does! Insert this method somewhere above all the popover action methods:

- (void)setupNewPopoverControllerForViewController:(UIViewController *)vc {
if (self.currentPopover) {
[self.currentPopover dismissPopoverAnimated:YES];
[self handleDismissedPopoverController:self.currentPopover];
}
self.currentPopover = [[[UIPopoverController alloc] initWithContentViewController:vc]
autorelease];
self.currentPopover.delegate = self;
}


We'll use this method every time we're going to present a popover. By doing so, we save a few lines in each popover action method, and also ensure that we're doing things the same way each time. Now, I'm not going to pretend that this separate method sprang from my forehead in one piece.

Any time you find yourself doing cut-and-paste coding, consider chunking things off into separate methods, because someone is probably going to revisit your code someday. The mind you save may be your own.


So now we have code in place to fire up the popover, but we still need to handle the user selecting something. Begin by creating a method to be called when the notification we're observing is triggered:

- (void)fontListControllerDidSelect:(NSNotification *)notification {
FontListController *flc = [notification object];
UIPopoverController *popoverController = flc.container;
[popoverController dismissPopoverAnimated:YES];
[self handleDismissedPopoverController:popoverController];
self.currentPopover = nil;
}

Here, you see the reason for putting the container property in FontListController. After the user makes a selection, FontListController shoots off a notification. DudelViewController picks it up, and uses the container property to dismiss the popover. This method also calls the main handler for all our popovers, which you should now revise to this:

- (void)handleDismissedPopoverController:(UIPopoverController*)popoverController {
if ([popoverController.contentViewController isMemberOfClass:
[FontListController class]]) {
// this is the font list, grab the new selection
FontListController *flc = (FontListController *)
popoverController.contentViewController;
self.font = [UIFont fontWithName:flc.selectedFontName size:self.font.pointSize];
}
self.currentPopover = nil;
}


This is where the selection in the popover finally ends up having an effect. The font is now set according to what the user picked.

You should now be able to build and run the app, and then touch the font list button in the toolbar to see something like the popup shown in Figure 2.

Try selecting a different font, and then using the Text tool to create some text. Neat! You now have the full complement of fonts included with the iPad at your disposal. You're still stuck with just one size, so let's tackle that next.

Figure 2. Selecting a font
Other  
  •  Beginning Android 3 : Working with Containers - Tabula Rasa
  •  Beginning Android 3 : Working with Containers - LinearLayout Example & The Box Model
  •  iPhone Application Development : Reading and Writing User Defaults (part 2) - Implementing System Settings
  •  iPhone Application Development : Reading and Writing User Defaults (part 1) - Creating Implicit Preferences
  •  - Mobile Application Security : SMS Security - Overview of Short Message Service
  •  - Mobile Application Security : Bluetooth Security - Bluetooth Security Features
  •  Integrating Your Application with Windows Phone 7
  •  Introducing Windows Phone 7 Photo Features (part 2) - Using a Chooser to Open Photos & Saving Photos to the Phone
  •  Introducing Windows Phone 7 Photo Features (part 1) - Using a Chooser to Take Photos
  •  Mobile Application Security : Bluetooth Security - Bluetooth Technical Architecture
  •  Mobile Application Security : Bluetooth Security - Overview of the Technology
  •  Windows Phone 7 Development : Push Notifications - Implementing Cloud Service to Track Push Notifications
  •  Windows Phone 7 Development : Push Notifications - Implementing Raw Notifications
  •  Windows Phone 7 Development : Push Notifications - Implementing Tile Notifications
  •  Windows Phone 7 Development : Push Notifications - Implementing Toast Notifications
  •  iPhone Application Development : Creating a Navigation-Based Application
  •  Windows Phone 7 Development : Push Notifications - Introducing the Push Notifications Architecture
  •  Windows Phone 7 Development : Push Notifications - Understanding Push Notifications
  •  Windows Phone 7 Development : Handling Multiple Concurrent Requests with Rx.NET
  •  WAP and Mobile HTML Security : Application Attacks on Mobile HTML Sites
  •  
    Top 10
    Nikon 1 J2 With Stylish Design And Dependable Image And Video Quality
    Canon Powershot D20 - Super-Durable Waterproof Camera
    Fujifilm Finepix F800EXR – Another Excellent EXR
    Sony NEX-6 – The Best Compact Camera
    Teufel Cubycon 2 – An Excellent All-In-One For Films
    Dell S2740L - A Beautifully Crafted 27-inch IPS Monitor
    Philips 55PFL6007T With Fantastic Picture Quality
    Philips Gioco 278G4 – An Excellent 27-inch Screen
    Sony VPL-HW50ES – Sony’s Best Home Cinema Projector
    Windows Vista : Installing and Running Applications - Launching Applications
    Most View
    Bamboo Splash - Powerful Specs And Friendly Interface
    Powered By Windows (Part 2) - Toshiba Satellite U840 Series, Philips E248C3 MODA Lightframe Monitor & HP Envy Spectre 14
    MSI X79A-GD65 8D - Power without the Cost
    Canon EOS M With Wonderful Touchscreen Interface (Part 1)
    Windows Server 2003 : Building an Active Directory Structure (part 1) - The First Domain
    Personalize Your iPhone Case
    Speed ​​up browsing with a faster DNS
    Using and Configuring Public Folder Sharing
    Extending the Real-Time Communications Functionality of Exchange Server 2007 : Installing OCS 2007 (part 1)
    Google, privacy & you (Part 1)
    iPhone Application Development : Making Multivalue Choices with Pickers - Understanding Pickers
    Microsoft Surface With Windows RT - Truly A Unique Tablet
    Network Configuration & Troubleshooting (Part 1)
    Panasonic Lumix GH3 – The Fastest Touchscreen-Camera (Part 2)
    Programming Microsoft SQL Server 2005 : FOR XML Commands (part 3) - OPENXML Enhancements in SQL Server 2005
    Exchange Server 2010 : Track Exchange Performance (part 2) - Test the Performance Limitations in a Lab
    Extra Network Hardware Round-Up (Part 2) - NAS Drives, Media Center Extenders & Games Consoles
    Windows Server 2003 : Planning a Host Name Resolution Strategy - Understanding Name Resolution Requirements
    Google’s Data Liberation Front (Part 2)
    Datacolor SpyderLensCal (Part 1)