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 will use a new screen with a text field.
To let the user input new names you will create a new view controller:
In the File menu, select New File....
With the iPhone OS Cocoa Touch Class selected on the left-hand side selection, choose UIViewController subclass.
Do not check the UITableViewController subclass option and select (check) the With XIB for user interface option.
Click Next.
Name the file NewNameViewController.m.
Select the Names/Classes/ location.
Click Finished.
This will create three files: the NewNameViewController header and implementation 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.
@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 will define the action later, once the DataAccess object can insert new names. For now, simply create an empty method stub 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), select the View.
In the Attribute Inspection Window (Command-1), under the Simulated 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 comfortably within it. A width of about 230 pixels should work. 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:
Select the text field in the view.
With the text field selected, open the Attribute Inspection Window (Command-1).
Set the Placeholder to Name.
Make the font size 18 points.
Set Capitalize to Words.
Set the Return Key to Done.
In order for the controller to be made aware of when the user is done entering the name, you must make action connections in Interface Builder.
Open the Connections Inspector (Command-2).
Click and drag from theDid End on Exit event circle to the File's Owner in the Document Window, and select done Adding to make the connection. The File's Owner of this XIB file is the NewNameViewController class, as the File's Owner's type indicates. 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.
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.
Select the 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 add 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:
- (void)showAddNameScreen; |
Implement the method with the following code:
- (void)showAddNameScreen { NewNameViewController * addNameScreen = [[NewNameViewController alloc] initWithNibName:@"NewNameViewController" bundle:nil]; [self.navigationController pushViewController:addNameScreen animated:YES]; } |
Similar to how you set the title for the NewNameViewController, set the title of the RootViewController, as well as add a plus-sign button to the right side of the navigation bar that calls showAddNameScreen.
- (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)]; } |
In order to insert the new names into the database, you will 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; |
To perform the insertion, the application will use the following SQL statement:
const char * INSERT = "INSERT INTO Names(name) VALUES(?)"; |
The addName method can now be implemented:
- (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, the NewNameViewController's doneAdding method can be implemented:
- (IBAction)doneAdding:(id)sender { if (newNameField.text > 0) { [[DataAccess sharedInstance] addName:newNameField.text]; } [self.navigationController popViewControllerAnimated:YES]; } |
You should now import the DataAccess header. The keyboard's done button now adds the name to the database and returns to the table view. However, the table view is not yet configured to display what is in the database.
Discuss this page in DocCommentXchange.
|
Copyright © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0 |