我正在创建一个电子邮件签名,到目前为止我已经制作了两个视图“ViewController 和“ListView”编码是这样的
视图控制器.h
#import <UIKit/UIKit.h>
#import "ListView.h"
#import "sqlite3.h"
@interface ViewController:UIViewController<UITextViewDelegate,UIImagePickerControllerDelegate>
{
UIImageView *imageView;
UIImage *signimage;
UIImagePickerController *imagePicker;
NSMutableArray *hello;
NSMutableArray *hellocontent;
ListView *listview;
UITextField *signaturename;
UITextView *textView; // UITextView *scontent;
// IBOutlet UIScrollView *scrollview;
// BOOL keyboardIsShown;
//IBOutlet UITextField *recordTextField;
NSString *databasePath;
sqlite3 *Dummy2;
}
@property(nonatomic,retain) IBOutlet UIImageView *imageView;
@property(nonatomic,retain) UIImage *signimage;
-(IBAction)btnLoadImage:(id) sender;
-(IBAction)clearFields:(id)sender;
-(IBAction)show:(id)sender;
// @property (nonatomic,retain) UITextField *recordsTextField;
//@property(nonatomic,retain) IBOutlet UIScrollView *scrollview;
@property(nonatomic, retain) IBOutlet UITextField *signaturename;
@property(nonatomic, retain) IBOutlet UITextView *textView; // scontent
-(IBAction) btnsave:(id) sender;
-(IBAction) keyboard:(id) sender;
-(IBAction)next:(id)sender;
-(IBAction) bgtouch:(id) sender;
// -(IBAction)show:(id)sender;
// -(IBAction)records:(id)s;
// -(IBAction)updateQuery:(id)sender;
//Static methods.
+ (void) getInitialDataToDisplay:(NSString *)dbPath;
+ (void) finalizeStatements;
//Instance methods.
- (id) initWithPrimaryKey:(NSInteger)pk;
- (void) saveAllData;
@end
视图控制器.m
#import "ViewController.h"
#import"ListView.h"
@implementation ViewController
@synthesize signaturename,imageView,textView,signimage; // recordsTextField
// scrollview;
-(IBAction)next:(id)sender{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
hello = [[NSMutableArray alloc]init];
// hellocontent = [[NSMutableArray alloc]init];
if (sqlite3_open(dbpath, &Dummy2) == SQLITE_OK)
{
NSString *updateSQL = @"SELECT * FROM PROFILE";
const char *update_stmt = [updateSQL UTF8String];
sqlite3_prepare_v2(Dummy2, update_stmt, -1, &statement, NULL);
while(sqlite3_step(statement) == SQLITE_ROW) {
[hello addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)]];
// [hellocontent addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)]];
}
sqlite3_finalize(statement);
sqlite3_close(Dummy2);
}
listview = [[ListView alloc]initWithNibName:@"ListView" bundle:nil];
listview.arr=[[NSMutableArray alloc]init];
listview.arr = hello;
[self.view addSubview:listview.view];
}
- (void)setCoffeeImage:(UIImage *)theCoffeeImage {
// self.isDirty = YES;
[signimage release];
signimage = [theCoffeeImage retain];
}
-(IBAction) btnsave:(id) sender
{
if (([self.signaturename.text length] && [self.textView.text length]) != 0) {
// NSData *data = UIImagePNGRepresentation(self.signimage);
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
hello = [[NSMutableArray alloc]init];
if (sqlite3_open(dbpath, &Dummy2) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO profile (sname,scontent) VALUES (\"%@\", \"%@\")", signaturename.text,textView.text];
const char *insert_stmt = [insertSQL UTF8String];
// sqlite3_bind_blob(statement, 3, [data bytes], [data length], NULL);
sqlite3_prepare_v2(Dummy2, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
// status.text = @"Contact added";
signaturename.text = @""; //address.text = @"";
textView.text = @"";
// imageUrl.text=@"";
} else {
// status.text = @"Failed to add contact";
}
sqlite3_finalize(statement);
sqlite3_close(Dummy2);
}
}
else
{
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Warning" message:@"Signature Name and Content Field Should Not Be Empty" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
}
-(IBAction)show:(id)sender{
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &Dummy2) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"SELECT sname, scontent FROM profile WHERE sname=\"%@\"", signaturename.text];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(Dummy2, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
textView.text = addressField;
// status.text = @"Match found";
[addressField release];
} else {
// status.text = @"Match not found";
textView.text = @"";
}
sqlite3_finalize(statement);
}
sqlite3_close(Dummy2);
}
}
-(IBAction)clearFields:(id)sender
{
signaturename.text=@"";
textView.text=@"";
NSLog(@"clear is working");
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
-(IBAction) bgtouch:(id) sender
{
[signaturename resignFirstResponder];
// [textView resignFirstResponder];
}
-(IBAction) keyboard:(id)sender
{
[sender resignFirstResponder];
// [self.signaturecontent resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
imagePicker =[[UIImagePickerController alloc]init];
[super viewDidLoad];
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"Dummy2.sqlite"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &Dummy2) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS profile(ID INTEGER PRIMARY KEY AUTOINCREMENT, SNAME TEXT, SCONTENT TEXT)";
if (sqlite3_exec(Dummy2, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
// status.text = @"Failed to create table";
}
sqlite3_close(Dummy2);
} else {
// status.text = @"Failed to open/create database";
}
}
[filemgr release];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)btnLoadImage:(id)sender
{
imagePicker.delegate =self;
imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
// show the image picker
[self presentModalViewController:imagePicker animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image;
NSURL *mediaUrl;
mediaUrl =(NSURL *)[info valueForKey:UIImagePickerControllerMediaURL];
if(mediaUrl == nil){
image = (UIImage *)[info valueForKey:UIImagePickerControllerEditedImage];
if(mediaUrl == nil){
// original image selected
image =(UIImage *)[info valueForKey:UIImagePickerControllerOriginalImage];
// display the image
imageView.image = image;
}
else {
// edited image picked
CGRect rect = [[info valueForKey:UIImagePickerControllerCropRect]CGRectValue];
// display the image
imageView.image = image;
}
}
// hide the image picker
[picker dismissModalViewControllerAnimated:YES];
}
-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
// user did not select image; hide the image picker
[picker dismissModalViewControllerAnimated:YES];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
列表视图.h
#import <UIKit/UIKit.h>
@interface ListView : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *arr;
}
@property(nonatomic,retain) NSMutableArray *arr;
-(IBAction)back:(id)sender;
@end
列表视图.m
#import "ListView.h"
@implementation ListView
@synthesize arr;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(IBAction)back:(id)sender
{
[super.view removeFromSuperview];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// return 6;
return [arr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSString *cellvalue = [arr objectAtIndex:indexPath.row];
cell.textLabel.text=cellvalue;
// Configure the cell.
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
数据库“Dummy2.sql”是这样的
PROFILE = table
id = field = INTEGER PRIMARY KEY
sname = field = TEXT
scontent = field = TEXT
simage = field = BLOB
Schema
CREATE TABLE PROFILE(id INTEGER PRIMARY KEY, sname TEXT, scontent TEXT, simage BLOB)
这就是我到目前为止所做的,我应该在 didselectrowAtIndexpath 中编码什么,以便当我从表中选择任何名称时,它应该出现在“ListView.xib”的文本视图中......任何帮助我也无法将图像保存在DB我还必须根据从“ListView.xib”中的tableView中选择的名称显示图像,以便在底部我可以在UIimage和textview字段中看到该名称的图像和内容..有什么想法吗??