0

我在弄清楚调用其他类中的方法时遇到了一些麻烦

#import "myNewClass.h"
#import "MainViewController.h"

@implementation MainViewController

@synthesize txtUsername;
@synthesize txtPassword;
@synthesize lblUserMessage;

- (IBAction)calculateSecret {

NSString *usec = [self calculateSecretForUser:txtUsername.text 
                       withPassword:txtPassword.text]; 

    [lblUserMessage setText:usec];
    [usec release];
} 
...

myNewClass.h

#import <Foundation/Foundation.h>

@interface myNewClass : NSObject {
}
- (NSString*)CalculateSecretForUser:(NSString *)user withPassword:(NSString *)pwd;

@end

我的新类.m

#import "myNewClass.h"

@implementation myNewClass

- (NSString*)CalculateSecretForUser:(NSString *)user withPassword:(NSString *)pwd
{
    NSString *a = [[NSString alloc] initWithFormat:@"%@ -> %@", user, pwd]; 
    return a;
}

@end

CalculateSecretForUser 方法总是说

“MainViewController”可能不响应“-calculateSecretForUser:withPassword:”

我在这里做错了什么?

4

1 回答 1

2

关键字“self”表示当前类的实例。所以你发送的消息没有实现它calculateSecretForUser:withPasswordMainViewController您应该实例化myNewClass并调用它:

- (IBAction)calculateSecret {
    myNewClass *calculator = [[myNewClass alloc] init];

    NSString *usec = [calculator calculateSecretForUser:txtUsername.text 
                        withPassword:txtPassword.text]; 

    [lblUserMessage setText:usec];
    [usec release];
    [calculator release];
} 
于 2010-04-05T07:39:46.073 回答