当我在这样的另一个类的方法中调用方法时我遇到问题当有人点击按钮时我有按钮的方法 //在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.h中我定义了此
的属性@property (strong) ViewController * callingViewController;
如果密码和用户名成功匹配,如果在lognow方法中有条件,我在Viewcontroller.com文件中调用此方法,但我无法调用 //ViewController.m
- (void)addview {
DBHelper *f = [[DBHelper alloc] init];
f.callingViewController = self;
newview.center = CGPointMake(1000, 1000);
}
答案 0 :(得分:0)
虽然在DBhelper中保持viewController是不明智的(它会破坏MVC),但您可以将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];
}
现在,当你成功登录时, - (void)DBHelp:(DBHelper *)helper didFinishedLoginSuc:(BOOL)将调用ViewController.m中的suc,你可以处理你的视图。
记得在viewController获取dealloc时将委托设置为nil,否则您会发现内存错误。小心。