Textfields似乎无缘无故被禁用。目标c xcode

时间:2013-02-05 04:33:25

标签: objective-c xcode

似乎我第二次在我的应用上插入/注册一个帐户。登录和注册页面上的文本字段似乎已禁用,因为我无法在其上单击或键入任何内容。这里有谁知道这是什么问题?这是我的代码。

登录:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize tbPassword,tbUsername;
-(void) createTable: (NSString *) UserTable
      withUserField: (NSString *) UserField
      withPassField: (NSString *) PassField;
{
    char *err;
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' " "TEXT PRIMARY KEY, '%@' TEXT);", UserTable, UserField,PassField];
    if (sqlite3_exec(users, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK) {
        sqlite3_close(users);
        NSAssert(0, @"Could not create table");

    }
    else{
        NSLog(@"table created");
    }
}

-(NSString *) filePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0] stringByAppendingPathComponent:@"users.sql"];
}

-(void) openDB {
    if (sqlite3_open([[self filePath]UTF8String],&users) != SQLITE_OK){
        sqlite3_close(users);
        NSAssert(0, @"Database failed to open");
    }
    else{
        NSLog(@"database opened");
    }
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [self openDB];
    [self createTable:@"UserAccount" withUserField:@"Username" withPassField:@"Password"];
    self.tbUsername.delegate=self;
    self.tbPassword.delegate=self;
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnLogin:(id)sender {
    NSString *select = [NSString stringWithFormat:@"SELECT * FROM UserAccount WHERE Username = '%s' COLLATE NOCASE and Password = '%s'",[tbUsername.text UTF8String],[tbPassword.text UTF8String]];
    sqlite3_stmt *statement;
    if (sqlite3_prepare(users, [select UTF8String], -1, &statement, nil)==SQLITE_OK)
    {
        if(sqlite3_step(statement)==SQLITE_ROW)
        {
            ViewController3 *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController3"];
            VC.strUsername = self.tbUsername.text;
            [self presentViewController:VC animated:YES completion:nil];

        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Authentication Failed" message:@"Wrong Username/Password!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }

    }
}

- (IBAction)btnRegister:(id)sender {
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [[self tbUsername]resignFirstResponder];
    [[self tbPassword]resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}
@end

寄存器:

#import "ViewController2.h"

@interface ViewController2 ()

@end

@implementation ViewController2
@synthesize tbPassword,tbRepass,tbUsername;
-(void) createTable: (NSString *) UserTable
      withUserField: (NSString *) UserField
      withPassField: (NSString *) PassField;
{
    char *err;
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' " "TEXT PRIMARY KEY, '%@' TEXT);", UserTable, UserField,PassField];
    if (sqlite3_exec(users, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK) {
        sqlite3_close(users);
        NSAssert(0, @"Could not create table");

    }
    else{
        NSLog(@"table created");
    }
}

-(NSString *) filePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0] stringByAppendingPathComponent:@"users.sql"];
}

-(void) openDB {
    if (sqlite3_open([[self filePath]UTF8String],&users) != SQLITE_OK){
        sqlite3_close(users);
        NSAssert(0, @"Database failed to open");
    }
    else{
        NSLog(@"database opened");
    }
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [self openDB];
    [self createTable:@"UserAccount" withUserField:@"Username" withPassField:@"Password"];
    self.tbUsername.delegate=self;
    self.tbPassword.delegate=self;
    self.tbRepass.delegate=self;
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnRegister:(id)sender
{
    NSString *select = [NSString stringWithFormat:@"SELECT * FROM UserAccount WHERE Username = '%s' COLLATE NOCASE",[tbUsername.text UTF8String]];
    sqlite3_stmt *statement;
    if (sqlite3_prepare(users, [select UTF8String], -1, &statement, nil)==SQLITE_OK)
    {
        if(sqlite3_step(statement)==SQLITE_ROW)
        {

            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Username already exist!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];

        }
        else
        {
           if ([tbUsername.text isEqualToString:@""])
            {

                UIAlertView *alert2 = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please enter a username." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert2 show];
            }
            else if([tbPassword.text length]==0 || [tbRepass.text length]==0)
            {

                UIAlertView *alert3 = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please enter a password." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert3 show];
            }
            else if([tbPassword.text length]>0 && [tbRepass.text length]>0)
            {

                if([tbPassword.text isEqualToString:tbRepass.text])
                {
                    NSLog(@"Password Match");
                    NSString *username = tbUsername.text;
                    NSString *pass = tbPassword.text;

                    NSString *sql = [NSString stringWithFormat:@"INSERT INTO UserAccount ('Username','Password') VALUES ('%@','%@')",username,pass];
                    char *err;
                    if (sqlite3_exec(users, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK)
                    {
                        sqlite3_close(users);
                        NSAssert(0, @"Could not update table)");

                    }
                    else
                    {
                        NSLog(@"table updated");
                        UIAlertView *alert4 = [[UIAlertView alloc]initWithTitle:@"Done" message:@"Account was registered successfully!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                        [alert4 show];
                        ViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
                        VC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

                       [self presentViewController:VC animated:YES completion:nil];

                        tbUsername.text = @"";
                        tbPassword.text = @"";


                    }

                }

                else
                {
                    UIAlertView *alert5 = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Password does not match." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alert5 show];
                }
            }
        }
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [[self tbUsername]resignFirstResponder];
    [[self tbPassword]resignFirstResponder];
    [[self tbRepass]resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}
@end

3 个答案:

答案 0 :(得分:1)

从touchesbegan中删除这些行

[[self tbUsername]resignFirstResponder];
    [[self tbPassword]resignFirstResponder];

这些线条会破坏键盘

答案 1 :(得分:0)

我刚刚注意到当我成功将数据插入数据库时​​。当我的键盘仍然可见时,页面会将我重定向到登录。我不确定我是否正确,但当我将resignFirstResponder添加到我的btnRegister时,它将我重定向到登录。文本字段仍然有用!谢谢btw

答案 2 :(得分:0)

UITapGestureRecognizer * gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)]; [self.view addGestureRecognizer:gestureRecognizer];

  • (void)hideKeyboard { [[self tbUsername] resignFirstResponder]; [[self tbPassword] resignFirstResponder]; }

你也可以在创建它的obj替换

之后创建新的UIImageView

[self.view addGestureRecognizer:gestureRecognizer];

[UIImageViewObj addGestureRecognizer:gestureRecognizer];