视图控制器中缺少方法声明

时间:2013-03-28 07:04:39

标签: objective-c uiviewcontroller

我已将其输入我的.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文件的上面代码是其中的所有代码,我已删除了上面和下面的其他位。

1 个答案:

答案 0 :(得分:2)

这是因为当您从viewDidLoad而不是NSObject继承StudentLoginViewController时会调用UIViewController。 NSObject本身就是一个超类,所以它没有任何父类,它声明了viewDidLoad。所以你得到错误:

[super viewDidLoad];

写:

@interface StudentLoginViewController : UIViewController <UITextFieldDelegate>

而不是

@interface StudentLoginViewController : NSObject <UITextFieldDelegate>