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
  •  
    Top 10
    A Look At Truecrypt The Open Source Security Tool
    Price Of Piracy
    Acer Aspire 5600U 23" Touchscreen All-in-One PC
    Zalman FX100-Cube Fanless Cooler
    Devolo dLAN LiveCam Starter Kit
    Has Apple Lost It? (Part 2)
    Has Apple Lost It? (Part 1)
    Sony Computer Entertainment (Part 3)
    Sony Computer Entertainment (Part 2)
    Sony Computer Entertainment (Part 1)
    Most View
    In Control
    Overclock Your Core i5 (Part 2)
    Using Non-Windows Systems to Access Exchange Server 2010 : Understanding Other Non-Windows Client Access Methods
    Grouptest Headphones: $150-$210 - Phone Home (Part 7)
    Humax DTR-T1000 – Freeview HD PVR With YouView
    Nvidia Quadro K5000 Professional Graphics Card (Part 1)
    Canon EOS-1 DX (Part 1)
    Separating BPM and SOA Processes : BPM-Oriented Disputes with TIBCO (part 2) - BusinessWorks Orchestration Processes & ActiveMatrix ESB Processes
    Biggest tips guide ever! (Part 8)
    All You Need To Know About iOS 6 (Part 1)
    How To Specify And Build A Media PC (Part 5)
    Tracing the iPhone Application Life Cycle
    Windows 7 : Windows Driver Foundation Architecture (part 4) - Tools for Development and Testing
    Sony SRS-D8 Evaluation
    ECS Z77H2-A2X v1.0 - Golden LGA 1155 Mainboard From The Black Series (Part 2)
    Popular GPS Apps Shootout (Part 5)
    Is The Personal Blog Dead? (Part 1) - The Rise Of Social Media
    Hit List: Current iPad And iPhone Line-Ups (Part 2)
    SQL Server 2008 : Explaining Advanced Query Techniques - Controlling Execution Plans (part 2)
    Programming with DirectX : Shading and Surfaces - Implementing Texture Mapping (part 1) - 2D Texture Mapping Demo