Now that the application has a database on the device with its schema initialized, it can start adding names to the database. To do this, the application uses a new screen with a text field.
To let the user input new names you create a new view controller.
Ctrl-click Classes and from the Add File menu, click New File.
With the iPhone OS Cocoa Touch Class selected on the left selection, click UIViewController subclass.
Do not click UITableViewController subclass option and click With XIB for user interface.
Click Next.
Name the file NewNameViewController.m.
Click the names/Classes/ location.
Click Finish.
This creates three files: the NewNameViewController (.h) header and implementation (.m) file, as well as a XIB file that can be edited in Interface Builder. To group all the XIB files together, you can move it to the Resources folder where two XIB files already exist (the MainWindow and RootViewController's XIB files).
Before editing the XIB file to contain the text field, you must first add an Outlet property to the header so that you can tie in the text field from the XIB file into the code as well as an action to perform when the user has finished entering the name. Add the following to the NewNameViewController.h file
@interface NewNameViewController : UIViewController { UITextField *newNameField; } @property (retain) IBOutlet UITextField *newNameField; - (IBAction)doneAdding:(id)sender; @end |
Synthesize the property in the implementation file and release the text field in the dealloc method. You define the action later, once the DataAccess object can insert new names. For now, simply create an empty method stub in NewNameViewController.m to avoid compilation warnings.
@synthesize newNameField; - (IBAction)doneAdding:(id)sender {} |
The IBAction keyword lets Interface Builder know that the method should be made available for events to call. Make sure to save the header so that Interface Builder knows about the method.
Now that the outlet is set up, double-click the NewNameViewController XIB file to edit it in Interface Builder. In Interface Builder, you should see an empty view with a status bar showing the battery charging. Since this view also has a navigation bar showing, simulate showing it by doing the following:
In the Document Window (Command-0), click View.
In the Attributes Inspector Window (Command-1), under the Simulated User Interface Elements, set the Top Bar to Navigation Bar.
The view should now show an empty navigation bar just below the status bar.
To allow the user to input a new name, add a text field to the view:
In the Library window (Command-Shift-L), under Inputs & Values, click and drag a text field into the view.
Position the text field in the upper half of the view, so that the keyboard in the lower half won't hide it.
Make the text field a little wider so that a name would fit within it. Use a width of about 230 pixels. To see the size and placement options, use the Size Inspector (Command-3) while the text field is selected.
To make the text field more user-friendly, you can change a few properties:
Click the text field in the view.
With the text field selected, open the Attributes Inspector Window (Command-1).
Set the Placeholder to Name.
Make the font size 18 points.
Set Capitalize to Words.
Set the Return Key to Done.
For the controller to be made aware of when the user has finished entering the name, you must make action connections in Interface Builder.
With the text field selected, open the Text Field Connections Inspector (Command-2).
Click and drag from the Did End on Exit event circle to the File's Owner in the Document Window, and click done Adding to make the connection. If dragging and dropping over the File's Owner doesn't provide a selection, make sure the NewNameViewController header is saved and has the IBOutlet doneAdding. The File's Owner of this XIB file is the NewNameViewController class, as the File's Owner's type indicates.
In order to refer to the text field in the code, you need to connect this text field to the IBOutlet property you defined in the header earlier.
Click File's Owner in the Document Window. Open the Connections Inspector (Command-2).
Under Outlets, look for newNameField.
Click and drag from the newNameField circle to the actual name field in the view.
The view is now complete. Save the XIB file in Interface Builder and return to Xcode.
To finish the view, you need to set a few more properties in code. Uncomment the viewDidLoad method template in the NewNameViewController implementation and replace it with the following code:
- (void)viewDidLoad { [super viewDidLoad]; // Set the title to display in the nav bar self.title = @"Add Name"; // Set the text field to the first responder to display the keyboard. // Without this the user needs to tap on the text field. [newNameField becomeFirstResponder]; } |
To display the view controller you just created, the RootViewController needs to be configured. Import the NewNameViewController and DataAccess headers in the RootViewController's implementation file and add the following method signature to its header (RootViewController.h):
- (void)showAddNameScreen; |
Implement the method in RootViewController.m with the following code:
- (void)showAddNameScreen { NewNameViewController * addNameScreen = [[NewNameViewController alloc] initWithNibName:@"NewNameViewController" bundle:nil]; [self.navigationController pushViewController:addNameScreen animated:YES]; } |
Set the title of the RootViewController (the same way you set the title for the NewNameViewController), as well as add a plus-sign button to the right side of the navigation bar that calls showAddNameScreen. Uncomment the viewDidLoad block and replace it with the following code:
- (void)viewDidLoad { [super viewDidLoad]; // The Navigation Controller uses this to display the title in the nav bar. self.title = @"Names"; // Little button with the + sign on the right in the nav bar self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showAddNameScreen)]; } |
To insert the new names into the database, you add functionality to the DataAccess object. Add the following method signature to the DataAccess header:
// Adds the given name to the database. - (void)addName:(NSString *)name; |
Implement the addName method in the implementation file:
- (void)addName:(NSString *)name { const char * INSERT = "INSERT INTO Names(name) VALUES(?)"; ULPreparedStatement * prepStmt = connection->PrepareStatement(INSERT); if (prepStmt) { // Convert the NSString to a C-Style string using UTF8 Collation prepStmt->SetParameterString(1, [name UTF8String], [name length]); prepStmt->ExecuteStatement(); prepStmt->Close(); connection->Commit(); } else { NSLog(@"Could not prepare INSERT statement."); } } |
Now that the addName method is complete, add the NewNameViewController's doneAdding method to the implementation file. Replace the method with the following code:
- (IBAction)doneAdding:(id)sender { if (newNameField.text > 0) { [[DataAccess sharedInstance] addName:newNameField.text]; } [self.navigationController popViewControllerAnimated:YES]; } |
Import the DataAccess header in NewNameViewController.h. This allows the keyboard's Done button to add the name to the database and return to the table view at runtime. However, the table view is not yet configured to display what is in the database. See Lesson 4: Displaying data from the database.
At this point you should build the application to test that it builds without errors. From the Build menu, click Build.
Discuss this page in DocCommentXchange.
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |