当我在像这样的另一个类的方法中调用方法时,我遇到了问题,当有人单击按钮//在 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);
}