当我尝试输入文本字段时,我收到EXC_BAD_ACCESS(code = 2 address = 0x0)错误

时间:2014-01-16 05:13:45

标签: ios objective-c

我是一名从远方国家学习英语和Objective-C的学生。

以下代码是我在textfield中输入内容时的测试代码。然后立即在命令行上打印一个char。

但是当我在正在运行的app上触摸textfield时。发生模糊错误。

#import "SimpleViewController.h"
#import "SimpleTextFieldDelegate.h"

@interface SimpleViewController ()

@end

@implementation SimpleViewController

@synthesize field;
@synthesize label;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   // Nothing special in here.
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"Do this will be printed on command line?");
    [self.field setDelegate: [[SimpleTextFieldDelegate alloc] init]];
}

@end

这是SimpleViewController类的实现代码。

int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
                                                  <- error caused at here.
    }
}

为什么我收到此错误?我无法猜到。

1 个答案:

答案 0 :(得分:1)

#import "SimpleViewController.h"
#import "SimpleTextFieldDelegate.h"

@interface SimpleViewController ()
@property SimpleTextFieldDelegate MySimpleTextFieldDelegate;
@end

@implementation SimpleViewController

@synthesize field;
@synthesize label;
@synthesize MySimpleTextFieldDelegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

//这里没什么特别的。     }

- (void)viewDidLoad
{
[super viewDidLoad];

MySimpleTextFieldDelegate = [[SimpleTextFieldDelegate alloc] init];

NSLog(@"Do this will be printed on command line?");
[self.field setDelegate:MySimpleTextFieldDelegate];
}