第二次使用时,IBAction按钮崩溃而不改变视图

时间:2013-09-17 00:07:20

标签: ios objective-c

所以我正在构建一个笔记应用程序,在点击按钮后将笔记保存到NSUSerDefaults。好吧,当我加载该视图并添加注释并单击按钮以便第一次保存时,它可以正常工作。但是,如果我不离开视图并添加新消息并尝试单击按钮进行保存,我的应用程序崩溃了。我真的不明白为什么。我现在使用的xcode版本没有给我很多关于我的错误的详细信息,但我确实看到了这个

0x00015a9b  <+1175>  xor    %eax,%eax
Program received signal 'SIGABRT'

这是按钮点击(发送事件)后触发的IBAction代码

-(IBAction)saveNote{
NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];
//If empty add text to synthesized array and then add the array to the USer defaults
if([[prefs stringArrayForKey:@"Subjects"] count]==0){
    NSLog(@"Went through here");
    [notesSubject addObject:writeNote.text];
    [prefs setObject:notesSubject forKey:@"Subjects"];

    NSLog(@"The length of the array is %d",[notesSubject count]);

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Note Saved" message:@"Your note was saved" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    //[notesView.tableView reloadData];

}
//If not empty, get the array stored in the user defaults and set it to a temp array, add text to that temp array and then place the temp array back to the user defaults.
else{
    newSubjects=[[NSMutableArray alloc]init];
    beforeSubjects=[[NSArray alloc]init];

    newSubjects= [prefs stringArrayForKey:@"Subjects"];
    [newSubjects addObject:writeNote.text];
    [prefs setObject:newSubjects forKey:@"Subjects"];
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Note Saved" message:@"Your note was saved" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    //[notesView.tableView reloadData];
}


}

是的,我真的不明白为什么应用程序会在第二次点击时崩溃。

1 个答案:

答案 0 :(得分:0)

你用这一行用一个不可变的实例覆盖了mutable newSubjects实例:

  

newSubjects = [prefs stringArrayForKey:@“Subjects”];

相反,它应该是这样的:

  

newSubjects = [[prefs stringArrayForKey:@“Subjects”] mutableCopy];