0

当我在像这样的另一个类的方法中调用方法时,我遇到了问题,当有人单击按钮//在 ViewController.m 按钮方法中时,我有按钮方法

-(IBAction)login:(id)sender
{
    DBHelper *objdb = [[DBHelper alloc] init];
    [objdb loginnow:textname.text andpassword:textpassword.text]; 
}

并且此按钮方法在 DBhelper.m 文件中调用此方法并成功调用此方法

-(void) loginnow:(NSString *) username andpassword:(NSString *) password
{
    [self createEditableCopyOfDatabaseIfNeeded];
    [self initializeDatabase];
    const char *sql;

    NSString *querySQL = [NSString stringWithFormat: @"SELECT username, password FROM CONTACT WHERE username='%@' AND password='%@'",username,password]; 
    sql = [querySQL UTF8String];

    if (sqlite3_prepare_v2(database, sql, -1, &init_statement, NULL) != SQLITE_OK) {
        NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
    }


    while (sqlite3_step(init_statement) == SQLITE_ROW) 
    {
        NSLog(@"char sql = %s" ,sql);


        dbusername = [NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement,0)];
        dbpassword = [NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement,1)];


    }
    if ([dbusername isEqualToString:username] && [dbpassword isEqualToString:password])
    {
        //DBHelper.callingViewController = self;

        [self.callingViewController addview]; 
    }

    else if (dbusername != username ||  dbpassword != password)
    {

        NSLog(@"dbusername is = %@" ,dbusername);
        NSLog(@"dbpassword is = %@" ,dbpassword);
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Login Failed"
                              message:@"Username Or Password is not Correct"
                              delegate:nil
                              cancelButtonTitle:nil
                              otherButtonTitles:@"OK ", nil];
        [alert show];
        [alert release];
    }
    sqlite3_reset(init_statement);
    [self closeDatabase];

}

并在 DBhelper.hi 中为此定义属性

@property (strong) ViewController * callingViewController;

如果密码和用户名成功匹配,则在 lognow 方法中的条件下,我在 Viewcontroller.com 文件中调用此方法,但无法调用 //ViewController.m

-(无效)添加视图 {

DBHelper *f = [[DBHelper alloc] init];
f.callingViewController = self;

newview.center = CGPointMake(1000, 1000);

}

4

1 回答 1

0

尽管在 DBhelper 中保存 viewController 是不明智的(它会破坏 MVC),但您可以将 ViewController 的方法作为代码调用,但请记住将 ViewController 设置为将 ViewController 传递给 DBhelper。也许是这样的:

//ViewController.m
-(IBAction)login:(id)sender
{
    DBHelper *objdb = [[DBHelper alloc] init];
    [objdb loginnow:textname.text andpassword:textpassword.text viewController:self]; 
}

//DBHelper.m
-(void) loginnow:(NSString *) username andpassword:(NSString *)password viewController:(ViewController *)vc
{ ...

    if ([dbusername isEqualToString:username] && [dbpassword isEqualToString:password])
    {
        [vc addview]; 
    }
  ...
}

但实际上你应该在这里使用委托(或块或通知,但委托是大多数情况)。像这样:

在 DBHelper.h 中,@interface 之前,添加

@class DBHelper;
@protocol DBHelperDelegate <NSObject>
-(void) DBHelp:(DBHelper *)helper didFinishedLoginSuc:(BOOL)suc;
@end

在@interface 和@end 标签之间,添加(假设你没有使用ARC)@property (nonatomic, assign) id delegate;

在 DBHelper.m 中,在 @implementation 中,添加(假设您没有使用自动合成)

@synthesize delegate = _delegate;

现在,您可以更改 [self.callingViewController addview];至

if (self.delegate && [self.delegate responseToSelector:@selector(DBHelp:didFinishedLoginSuc:)]) {
    [self.delegate DBHelp:self didFinishedLoginSuc:YES];
}

现在,您为每个遵循 DBHelperDelegate 的视图控制器准备了一个委托。在您的 ViewController.h 中,通过在类声明后面添加来告诉编译器它服从 DBHelperDelegate:

@interface ViewController:UIViewController<DBHelperDelegate>

并将 addView 方法名称更改为

-(void) DBHelp:(DBHelper *)helper didFinishedLoginSuc:(BOOL)suc

最后,当您单击按钮时,将 self 设置为 objdb 的委托

-(IBAction)login:(id)sender
{
    DBHelper *objdb = [[DBHelper alloc] init];
    objdb.delegate = self;
    [objdb loginnow:textname.text andpassword:textpassword.text]; 
}

现在,当您成功登录时, ViewController.m 中的 -(void) DBHelp:(DBHelper *)helper didFinishedLoginSuc:(BOOL)suc 将被调用,您可以处理您的视图。

请记住,当您的 viewController 获得 dealloc 时,将委托设置为 nil,否则您会遇到内存错误。当心。

于 2012-12-06T08:46:34.163 回答