我有一个简单的笔记项目,我只有2个控制器:
NotesListViewController
CreateNotesViewController
在prepareForSegue
我的CreateNotesViewController
方法中,我将note属性设置为TextView文本属性,而不是我在unwindToList
方法中将其设置为NotesListViewController
1}}。
但是当我运行应用程序时,我在代码行中出现错误,我想将recieivedNote
添加到我的notes数组(在NotesListViewController中)。
我的视图控制器很短,所以我希望你不介意我分享它们: (我也只是学习核心数据基础并试图在这里实现它,这可能会导致问题)
CreateNotesViewController.m:
#import "CreateNotesViewController.h"
@interface CreateNotesViewController ()
@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;
@property (weak, nonatomic) IBOutlet UITextView *myTextView;
@end
@implementation CreateNotesViewController
- (NSManagedObjectContext *) managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]){
context = [delegate managedObjectContext];
}
return context;
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (sender != self.saveButton) return;
else if (self.myTextView.text.length > 0) {
[self.note awakeFromInsert];
self.note.content = self.myTextView.text;
NSManagedObjectContext *context = [self managedObjectContext];
// creating a new managed object
NSManagedObject *newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:context];
[newNote setValue:self.myTextView.text forKey:@"content"];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.myTextView.text = self.note.content;
// listen for keyboard hide/show notifications so we can properly adjust the table's height
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)adjustViewForKeyboardReveal:(BOOL)showKeyboard notificationInfo:(NSDictionary *)notificationInfo
{
// the keyboard is showing so ƒ the table's height
CGRect keyboardRect = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animationDuration =
[[notificationInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.myTextView.frame;
// the keyboard rect's width and height are reversed in landscape
NSInteger adjustDelta = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? CGRectGetHeight(keyboardRect) : CGRectGetWidth(keyboardRect);
if (showKeyboard)
frame.size.height -= adjustDelta;
else
frame.size.height += adjustDelta;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.myTextView.frame = frame;
[UIView commitAnimations];
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
[self adjustViewForKeyboardReveal:YES notificationInfo:[aNotification userInfo]];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
[self adjustViewForKeyboardReveal:NO notificationInfo:[aNotification userInfo]];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
NotesListViewController.m
#import "NotesListViewController.h"
#import "Note.h"
#import "CreateNotesViewController.h"
@interface NotesListViewController ()
@property (nonatomic, strong) NSMutableArray *notes;
@property (nonatomic) NSInteger editedRow;
@end
@implementation NotesListViewController
- (NSManagedObjectContext *) managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]){
context = [delegate managedObjectContext];
}
return context;
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"editSegue"]) {
CreateNotesViewController *destination = (CreateNotesViewController *)[segue destinationViewController];
NSInteger indx = [self.tableView indexPathForCell:sender].row;
Note *noteToPass = self.notes[indx];
destination.note = noteToPass;
self.editedRow = indx;
} else if ([[segue identifier] isEqualToString:@"addSegue"]) {
self.editedRow = -1;
}
}
- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
CreateNotesViewController *source = (CreateNotesViewController *)[segue sourceViewController];
Note *recieivedNote = source.note;
if (recieivedNote != nil && self.editedRow == -1) {
[self.notes addObject:recieivedNote];
} else {
//this is the line that i get the error in:
[self.notes replaceObjectAtIndex:self.editedRow withObject:recieivedNote];
}
[self.tableView reloadData];
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.notes = [[NSMutableArray alloc] init];
}
- (void) viewWillAppear
{
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Note"];
self.notes = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.notes.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSManagedObject *noteToDisplay = [self.notes objectAtIndex:indexPath.row];
//Note *noteToDisplay = [self.notes objectAtIndex:indexPath.row];
cell.textLabel.text = [noteToDisplay valueForKey:@"content"];
return cell;
}
@end
Note.h(这是模型,.m文件中没有任何内容)
@interface Note : NSManagedObject
@property (nonatomic, retain) NSString * content;
@end
请帮我弄清楚为什么我传递一个零物品......
错误截图:
答案 0 :(得分:0)
Nir,在您的NotesListViewController.m
和prepareForSegue:sender:
方法中,您包含以下代码行。
destination.note = noteToPass;
据我了解,在这行代码中,您尝试在目标视图控制器note
中设置属性CreateNotesViewController.m:
,但属性note
不存在。
查看包含note
公共属性(在CreateNotesViewController.h中声明 - 以便它是公共的,即可由其他类访问,更重要的是可以访问NotesListViewController类)。您还需要导入Note.h。
CreateNotesViewController.h文件中的两行额外代码将包含类似的内容......
#import "Note.h"
和
@property (nonatomic, strong) Note *note;
然后当你"设置"代码行destination.note = noteToPass
中的note属性,这样您就提供了一个公共属性来接收noteToPass
的值。
这有帮助吗?