2014-01-16 27 views
-1

我是一名從遠處國家學習英語和Objective-C的學生。當我嘗試在文本框中輸入時,出現EXC_BAD_ACCESS(代碼= 2地址= 0x0)錯誤

下面的代碼是測試代碼,當我在文本字段中鍵入內容時。然後立即在命令行上打印一個字符。

但是當我觸摸正在運行的應用程序的文本。發生模糊的錯誤。

#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. 
    } 
} 

爲什麼我得到這個錯誤?我無法猜出任何猜測。

+0

嘗試保留simpletextfielddelegate作爲視圖@property在@ JB13屬性代碼中的接口代碼完全存在於「SimpleViewController.h」接口 – JB13

+0

,但結果是相同的。 – Notice

+0

它看起來像。沒有什麼特別的可能導致錯誤。我剛剛初始化委託..,請幫助我.. – Notice

回答

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]; 
} 
+0

對不起,我在我的ipad上的格式 – JB13

+0

發佈答案後,我意識到你是幾乎相同的,所以我會刪除我的。不過,我鼓勵你加入一些小敘述,解釋爲什麼上述技術可行。我還可以建議(a)在實例化SimpleTextFieldDelegate對象時使用屬性setter而不是ivar;和(b)按照[可可命名約定](https://developer.apple.com/library/mac/documentation/cocoa/conceptual/codingguidelines/Articles/NamingIvarsAndTypes.html)採用駱駝小寫首字母作爲變量。變量的'simpleTextFieldDelegate'。但+1! – Rob

相關問題