由于应用程序在设备上有已初始化模式的数据库,所以可以开始将名称添加到数据库。为此,应用程序将使用含文本字段的新屏幕。
要使用户输入新名称,需要创建新的视图控制器。
按住 Ctrl 并单击 Classes,然后从 [Add File] 菜单中单击 [New File]。
从左侧选项选中 [iPhone OS Cocoa Touch Class] 的情况下,单击 [UIViewController] 子类。
不要单击 [UITableViewController] 子类选项,而单击 [With XIB for user interface]。
单击 [Next]。
将文件命名为 NewNameViewController.m。
单击 names/Classes/ 位置。
单击 [Finish]。
这将创建三个文件:NewNameViewController (.h) 标头和实现 (.m) 文件,以及可在 Interface Builder 中编辑的 XIB 文件。要将所有 XIB 文件聚集在一起,可将其移动到已有两个 XIB 文件(MainWindow 和 RootViewController 的 XIB 文件)的 Resources 文件夹。
在编辑 XIB 文件以包含文本字段前,首先必须将 Outlet 属性添加到标头,从而使来自 XIB 文件的文本字段与代码以及在用户完成名称输入时执行的操作关联在一起。将以下内容添加到 NewNameViewController.h 文件
@interface NewNameViewController : UIViewController { UITextField *newNameField; } @property (retain) IBOutlet UITextField *newNameField; - (IBAction)doneAdding:(id)sender; @end |
在实现文件中合成该属性,并在 dealloc 方法中释放文本字段。一旦 [DataAccess] 对象可插入新名称,您将定义以后的操作。目前,只需在 NewNameViewController.m 中创建空的方法存根以避免出现编译警告。
@synthesize newNameField; - (IBAction)doneAdding:(id)sender {} |
IBAction 关键字使 [Interface Builder] 了解应向事件提供该方法以供调用。确保保存标头,这样 [Interface Builder] 才能了解该方法。
由于出口已设置,所以可以双击 NewNameViewController XIB 文件,在 [Interface Builder] 中对其进行编辑。在 [Interface Builder] 中,应该可以看见一个空视图以及显示电池充电的状态栏。由于此视图还有导航栏显示,所以可通过以下操作模拟其显示:
在 [Document Window] (Command-0) 中,单击 [View]。
在 [Attributes Inspector Window] (Command-1) 中,于 [Simulated User Interface Elements] 下将 [Top Bar] 设置为 [Navigation Bar]。
视图现在应该在状态栏下方显示空的导航栏。
要允许用户输入新的名称,需要向视图添加文本字段:
在 [Library] 窗口 (Command-Shift-L) 中,在 [Inputs & Values] 下单击文本字段并将其拖放到视图中。
将文本字段定位在视图的上半部,这样位于下半部的键盘才不会将其挡住。
略微加宽文本字段的宽度,使名称与其匹配。使用大约 230 像素的宽度。要查看大小和布置选项,在选择文本字段的同时使用 [Size Inspector] (Command-3)。
要使文本字段更加便于用户使用,可以更改若干属性:
在视图中单击文本字段。
在选择文本字段的情况下,打开 [Attributes Inspector Window] (Command-1)。
将 [Placeholder] 设置为 [Name]。
将字号设为 18 磅。
将 [Capitalize] 设置为 [Words]。
将 [Return Key] 设置为 [Done]。
为了使控制器了解用户何时完成名称输入,必须在 [Interface Builder] 中创建操作连接。
在选择文本字段的情况下,打开 [Text Field Connections Inspector] (Command-2)。
从 [Did End on Exit] 事件圈中单击并拖动到 [Document Window] 中的 [File's Owner],然后单击 [done Adding] 以创建连接。如果拖放到 [File's Owner] 时未提供选择,请确保 NewNameViewController 标头已保存并且含有 IBOutlet doneAdding。此 XIB 文件的 [File's Owner] 为 NewNameViewController 类,如 [File's Owner's] 类型所示。
为了在代码中引用文本字段,需要将此文本字段连接到先前在标头中定义的 IBOutlet 属性。
在 [Document Window] 中单击 [File's Owner]。打开 [Connections Inspector](Command-2)。
在 [Outlets] 下查寻 newNameField。
从 newNameField 圈单击并拖动到视图中的实际名称字段。
视图现在已完成。将 XIB 文件保存到 [Interface Builder] 中并返回 Xcode。
要完成视图,需要在代码中再设置若干属性。在 [NewNameViewController] 实现中取消 [viewDidLoad] 方法模板的注释,并用以下代码替换它:
- (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]; } |
要显示刚才创建的视图控制器,需要配置 RootViewController。在 [RootViewController] 的实现文件中导入 [NewNameViewController] 和 [DataAccess] 标头,并将以下方法签名添加到其标头 (RootViewController.h):
- (void)showAddNameScreen; |
使用以下代码实现 RootViewController.m 中的方法:
- (void)showAddNameScreen { NewNameViewController * addNameScreen = [[NewNameViewController alloc] initWithNibName:@"NewNameViewController" bundle:nil]; [self.navigationController pushViewController:addNameScreen animated:YES]; } |
设置 [RootViewController] 的标题(与为 [NewNameViewController] 设置标题的方法相同),并且将一个加号按钮添加到名为 [showAddNameScreen] 的导航栏右侧。在 [viewDidLoad] 块中取消注释,并用以下代码将其替换:
- (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)]; } |
为了将新名称插入到数据库,需将功能添加到 [DataAccess] 对象。将以下方法签名添加到 DataAccess 标头:
// Adds the given name to the database. - (void)addName:(NSString *)name; |
在实现文件中实现 [addName] 方法:
- (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."); } } |
既然 [addName] 方法已完成,现在可将 [NewNameViewController] 的 [doneAdding] 方法添加到实现文件中。使用以下代码替换方法:
- (IBAction)doneAdding:(id)sender { if (newNameField.text > 0) { [[DataAccess sharedInstance] addName:newNameField.text]; } [self.navigationController popViewControllerAnimated:YES]; } |
导入 NewNameViewController.h 中的 [DataAccess] 标头。这将允许键盘的 [done] 按钮将名称添加到数据库,并在运行时返回到表视图。但是,表视图尚未配置显示数据库中的内容。请参见第 4 课:显示数据库的数据:。
在该点,您应构建应用程序以验证其是否可以正确构建。从 [Build] 菜单中,单击 [Build]。
![]() |
使用DocCommentXchange讨论此页。
|
版权 © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |