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