MOBILE

iPhone Application Development : Working with Text, Keyboards, and Buttons (part 5) - Implementing the View Controller Logic

1/16/2011 3:48:15 PM

Implementing the View Controller Logic

To finish off FieldButtonFun, we need to add the createStory method within the view controller (FieldButtonFunViewController). This method will search the template text for the <place>, <verb>, and <number> placeholders, and then replace them with the user’s input, storing the results in the text view. We’ll make use of the NSString instance method stringByReplacingOccurrencesOfString:WithString to do the heavy lifting. This method performs a search and replace on a given string.

For example, if the variable myString contains Hello town and you wanted to replace town with world, you might use the following:

myNewString=[myString stringByReplacingOccurrencesOfString:@"town" WithString:@"world"];

In this case, our strings are the text properties of the text fields and text views (thePlace.text, theVerb.text, theNumber.text, theTemplate.text, and theStory.text).

Add the final method implementation, shown in Listing 4, to FieldButtonFunViewController.m after the @synthesize directives.

Listing 4.
 1: -(IBAction) createStory:(id)sender {
2: theStory.text=[theTemplate.text
3: stringByReplacingOccurrencesOfString:@"<place>"
4: withString:thePlace.text];
5: theStory.text=[theStory.text
6: stringByReplacingOccurrencesOfString:@"<verb>"
7: withString:theVerb.text];
8: theStory.text=[theStory.text
9: stringByReplacingOccurrencesOfString:@"<number>"
10: withString:theNumber.text];
11: }

Lines 2–4 replace the <place> placeholder in the template with the contents of the thePlace field, storing the results in the story text view. Lines 5–7 then update the story text view by replacing the <verb> placeholder with the appropriate user input. This is repeated again in lines 8–10 for the <number> placeholder. The end result is a completed story, output in the theStory text view.

Releasing the Objects

When you’re done using an object in your applications, you should always release it to free up memory. This is good practice, even if the application is about to exit. In this application, we’ve retained six objects—each of the interface elements—that need to be released. Edit the dealloc method to release these now:

- (void)dealloc {
[thePlace release];
[theVerb release];
[theNumber release];
[theStory release];
[theTemplate release];
[generateStory release];
[super dealloc];
}

Our application is finally complete!

Building the Application

To view and test the FieldButtonFun, click Build and Run in Xcode. Your finished app should look very similar to Figure 19, fancy button and all!

Figure 19. The finished application includes scrolling views, text editing, and a pretty button! What more could we want?


This project provided a starting point for looking through the different properties and attributes that can alter how objects look and behave within the iPhone interface. The take-away message: Don’t assume anything about an object until you’ve reviewed how it can be configured.

Other  
  •  Building Android Apps : Controlling the Phone with JavaScript (part 3) - Accelerometer
  •  Building Android Apps : Controlling the Phone with JavaScript (part 2) - Geolocation
  •  Building Android Apps : Controlling the Phone with JavaScript (part 1) - Beep, Vibrate, and Alert
  •  Building Your First Windows Phone 7 Application (part 5) - Styling Your Application
  •  Building Your First Windows Phone 7 Application (part 4) - Customizing Your First Windows Phone Application
  •  Building Your First Windows Phone 7 Application (part 3) - Writing Your First Windows Phone Code
  •  Building Your First Windows Phone 7 Application (part 2) - Using Your First Windows Phone Silverlight Controls
  •  Building Your First Windows Phone 7 Application (part 1) - Creating a Windows Phone Project
  •  Introducing Windows Phone 7 and the Windows Phone Platform
  •  Windows Phone Application Platform
  •  iPhone Application Development : Basic User Input and Output
  •  Mobile Phone Game Programming : A Quick J2ME Primer
  •  Mobile Phone Game Programming : Java As a Mobile Game Platform
  •  Mobile Phone Game Programming : Getting to Know Mobile Platforms
  •  Mobile Application Security : The Apple iPhone - Local Data Storage: Files, Permissions, and Encryption
  •  Mobile Application Security : The Apple iPhone - Permissions and User Controls
  •  iPhone Application Developmen : Using the View-Based Application Template (part 3)
  •  iPhone Application Developmen : Using the View-Based Application Template (part 2) - Preparing the View Controller Outlets and Actions
  •  iPhone Application Developmen : Using the View-Based Application Template (part 1)
  •  Mobile Application Security: Application Format
  •  
    Most View
    Big Shot Rivals: Sony vs. Samsung
    500px - The Stunning Photography Gallery Comes To iPhone
    The Complete Guide To Photography On Your Mac! (Part 1)
    Installing the HP-UX 11i Operating Environment (part 1) - Integrity Early Boot and Begin Loading HP-UX from an Ignite-UX Server, Integrity Early Boot and Begin Loading HP-UX from Media
    Maingear Alpha 24 Super Stock - No Retreat, No Surrender
    Synology 4-Bay NAS: Three Models Review (Part 4)
    Get Yourself A Portable DAC
    HTC Butterfly - Smartphone With Full HD Display
    Windows 8 Explorer : Diagnosis and Recovery - The Processes Tab
    Microsoft Dynamics Sure Step 2010 : A repeatable process for the sales teams (part 2)
    Top 10
    Zotac GeForce GTX Titan Black
    Toshiba Q Series 256GB and 512GB, Toshiba Q Series Pro 256GB
    The Gaming Mouse: Ozone Neon
    SanDisk Ultra Plus 256GB And X110 256GB
    SanDisk Extreme II 480GB, Plextor M5 Pro Extreme 256GB and 512GB
    Samsung SSD 840 Evo 250GB, 500GB And 1TB
    PNY XLR8 240GB and XLR8 PRO 240GB
    OCZ Vector 150 240GB And 480GB, And Vertex 460 240GB
    Nvidia GeForce GTX 750 Ti
    Nokia Lumia 1020 41 Megapixels Of Awesome