0

我已将其输入到我的 .m 文件中 -StudentLoginViewController并且编译器不断给我错误:

“NOSbject”没有可见的@interface 声明选择器“viewDidLoad”

这是我的代码:

// StudentLoginViewController.h

#import <UIKit/UIKit.h>

@interface StudentLoginViewController : NSObject  <UITextFieldDelegate> {
    IBOutlet UILabel *Username ,*Password ;
    IBOutlet UIButton *LoginButton ;
    IBOutlet UITextField *UsernameText ,*PasswordText;
}

@end

// StudentLoginViewController.m

#import "StudentLoginViewController.h"

@interface StudentLoginViewController ()

@end

@implementation StudentLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UsernameText.delegate=self;
    PasswordText.delegate=self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // this text will not be updated to the newest text yet,
    // but we know what what the user just did to get into a string

    NSString *Username, *Password;
    if (textField == UsernameText) {
        Username = @"jzarate";
        Password = PasswordText.text; 
    } else {
        Username= UsernameText.text;
        Password = @"14054";
    }

    // If both the username and password are correct  then enable the button
    LoginButton.enabled = ([Username isEqualToString:@"jzarate"] && [Password isEqualToString:@"14054"]);

    // return YES so that the users edits  are used
    return YES;
}

@end

请注意,.m 文件上面的代码是其中的所有代码,我已经删除了上面和下面的其他位。

4

1 回答 1

2

这是因为当您将继承自而不是 NSObjectviewDidLoad时会调用它。NSObject 本身就是一个超类,所以它没有任何父类,父类声明 viewDidLoad。因此,您在以下位置遇到错误:StudentLoginViewControllerUIViewController

[super viewDidLoad];

写:

@interface StudentLoginViewController : UIViewController <UITextFieldDelegate>

代替

@interface StudentLoginViewController : NSObject <UITextFieldDelegate>

于 2013-03-28T07:12:39.427 回答