我剛開始學習iOS開發和Objective C和我下面的蘋果教程(https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/ThirdTutorial.html#//apple_ref/doc/uid/TP40011343-CH10-SW1)的UITextField不真正工作
我有一個文本字段,增加了一個新的任務到一個數組。當我按「完成」任務被添加到陣列,它出現在tableview中 - 但後來我得到這個在調試:
[XYZViewController addTaskField:]:無法識別的選擇發送到實例0x7fb13348c8c0 2014- 10-16 18:00:36.120 Tutorial123 [7880:1837804] ***終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因: ' - [XYZViewController addTaskField:]:無法識別的選擇發送到實例0x7fb13348c8c0'
以下是我在該文件中處理文本字段的代碼:
#import "XYZViewController.h"
#import "ToDoItem.h"
@interface XYZViewController()
@property (weak, nonatomic) IBOutlet UITextField *addTaskField;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *doneButton;
@end
@implementation XYZViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if(sender != self.doneButton) return;
if(self.addTaskField.text.length > 0){
self.todoItem = [[ToDoItem alloc] init];
self.todoItem.itemName = self.addTaskField.text;
self.todoItem.completed = NO;
}
}
@end
哪行代碼導致錯誤?如果您不知道,請參閱http://raywenderlich.com/10209/my-app-crashed-now-what-part-1 – rmaddy 2014-10-16 22:07:11
您是否已連接將您的IBOutlet稱爲addTaskField連接到故事板中的UITextField?我認爲是因爲addTaskField沒有連接到故事板內的任何文本字段,它是null,並且self.addTaskField.text爲null,所以要求self.addTaskField.text的長度會引發異常 – CjCoax 2014-10-16 22:24:55
我確信它是因爲在發生錯誤之前,我看到我輸入的文本填充在表中,但是在錯誤崩潰之後第二秒。因此,似乎所有的東西都被調用並且數組被更新了,但最終發生的事情會使所有的東西崩潰。 – 2014-10-16 22:57:38