2014-04-04 57 views
0

我有一个简单的笔记应用,在那里我有只有2视图控制器:如何传递一个对象而不是一个对象的细节?

  1. 表视图控制器 - 列出所有的笔记。
  2. 视图控制器 - 创建新的笔记。

在表格视图控制器中,我有一个从单元格切换回创建页面的位置,用户可以在该页面中编辑此特定单元格中的注释。

但我的问题是,当我在预成型编辑到某小区(注)我创建与我编辑的内容的新笔记...

因此,而不是通过注释内容在prepareForSegue方法中,我需要传递注释对象...

我该怎么做?

这是我的课:

NMNote:(正确只是包含内容*属性,将增加多后的行为)

#import <Foundation/Foundation.h> 

@interface NMNote : NSObject 

@property (strong, nonatomic) NSString *content; 


@end 

NMCreateNotesViewController.h:

#import <UIKit/UIKit.h> 
#import "NMNote.h" 

@interface NMCreateNotesViewController : UIViewController 

@property (strong, nonatomic) NMNote *note; 

@property (weak, nonatomic) IBOutlet UITextView *textField; 

@property (strong, nonatomic) NSString *passedInString; 


@end 

NMCreateNotesViewController .m:

#import "NMCreateNotesViewController.h" 
#import "NMNotesListViewController.h" 


@interface NMCreateNotesViewController() <UITextViewDelegate> 


@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton; 


@end 



@implementation NMCreateNotesViewController 


- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    // 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]; 
} 


#pragma mark - Notifications 


- (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.textField.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.textField.frame = frame; 
    [UIView commitAnimations]; 
} 



- (void)keyboardWillShow:(NSNotification *)aNotification 
{ 
    [self adjustViewForKeyboardReveal:YES notificationInfo:[aNotification userInfo]]; 
} 



- (void)keyboardWillHide:(NSNotification *)aNotification 
{ 
    [self adjustViewForKeyboardReveal:NO notificationInfo:[aNotification userInfo]]; 
} 



- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if (sender != self.saveButton) return; 
    if (self.textField.text.length > 0) { 
     self.note = [[NMNote alloc] init]; 
     self.note.content = self.textField.text; 

    } 
} 



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 



- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    if (self.passedInString != nil) { 
     self.textField.text = self.passedInString; 
    } 
    // Do any additional setup after loading the view. 
} 



- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

NMNotesListViewController.h:

#import <UIKit/UIKit.h> 

@interface NMNotesListViewController : UITableViewController 

- (IBAction) unwindToList: (UIStoryboardSegue *) segue; 

@end 

NMNotesListViewController.m:

#import "NMNotesListViewController.h" 
#import "NMCreateNotesViewController.h" 

@interface NMNotesListViewController() 

@property (strong, nonatomic) NSMutableArray *notes; 

@end 

@implementation NMNotesListViewController 



- (IBAction) unwindToList: (UIStoryboardSegue *) segue 
{ 

    NMCreateNotesViewController *source = [segue sourceViewController]; 
    NMNote *note = source.note; 

    if (note != nil) { 
     [self.notes addObject:note]; 
     [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]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    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 = @"NotesPrototypeCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // Configure the cell... 


    NMNote *note = [self.notes objectAtIndex:indexPath.row]; 
    cell.textLabel.text = note.content; 


    return cell; 
} 


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)sender 

{ 
    if ([[segue identifier] isEqualToString:@"noteSegue"]) { 
     NMCreateNotesViewController *destination = [segue destinationViewController]; 
     NSInteger indx = [self.tableView indexPathForCell:sender].row; 
     NMNote *note = self.notes[indx]; 
     destination.passedInString = note.content; 
    } 
} 

//#pragma mark - delegate 
// 
//- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
//{ 
//  
//} 

@end 

这是屏幕流量:

的发起视图此表视图:

enter image description here

现在有是你写的便条TextView的:

enter image description here

现在,为你节省一记后,你再回到第一个屏幕。然后你可以点击一个已填充的单元格,然后你将继续回到这个屏幕(带有TextView的那个),这样你就可以编辑它。但不是编辑它,它会创建一个新的编辑内容。像这样:

enter image description here

请,将在这里得到任何帮助来完成我的任务.. 谢谢!

+0

也许您应该缩小代码的范围,使其与问题更相关,而不是很多人想要筛选所有这些。 – JMarsh

+0

@JMarsh对不起。我只是想给人们足够的信息来帮助...我做到了,因为我虽然很短,对不起朋友。 – Joe

+0

@ user3412425你在哪里保存笔记? – meda

回答

1

时需要您通过照会NMCreateNotesViewController做的事情,是区分一个编辑和一个添加动作,所以当你回到表格视图时,你可以用新的编辑替换旧的条目,或者添加一个新的条目。

我会这样做的方式是有两个segues,一个来自+按钮(我称之为“addSegue”),另一个来自表视图单元格(称之为“editSegue”)。我还会在列表控制器中创建一个属性,以保存编辑行的值,或者将其设置为类似-1的值,以表示它是新注释。事情是这样的,

@property (nonatomic) NSInteger editedRow; 

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"editSegue"]) { 
     NMCreateNotesViewController *destination = [segue destinationViewController]; 
     NSInteger indx = [self.tableView indexPathForCell:(UITableViewCell *)sender].row; 
     self.editedRow = index; 
     NMNote *note = self.notes[indx]; 
     destination.note = note; 
    }else if ([segue.identifier isEqualToString:@"addSegue"]) { 
     self.editedRow = -1; 
} 

的prepareForSegue方法在NMCreateNotesViewController将是一样的,你在你的问题都有。您可以摆脱passedInString属性,因为我们传入的是整个音符对象。在列表控制器中的展开方法中,您可以这样做:

- (IBAction) unwindToList: (UIStoryboardSegue *) segue { 

    NMCreateNotesViewController *source = [segue sourceViewController]; 
    NMNote *note = source.note; 

    if (note != nil && self.editedRow == -1) { 
     [self.notes addObject:note]; 
    }else{ 
     [self.notes replaceObjectAtIndex:self.editedRow withObject:note]; 
    } 
    [self.tableView reloadData]; 
} 
+0

你已经做了一遍,最好的答案:)))它的作品完美,并向我解释了很多事情!谢谢@rdelmar – Joe

0
在NMCreateNotesViewController.h

@property (strong, nonatomic) NMNote *note; 
@property BOOL adding; 
@property (strong,nonatomic) NSString *originalContent; 
在NMNotesListViewController

然后。米prepareForSegue

destination.note=self.notes[indx]; 
destination.adding=NO;   // set adding to yes if you are adding a new note 

,并在您的开卷

- (IBAction) unwindToList: (UIStoryboardSegue *) segue 
{ 

    NMCreateNotesViewController *source = [segue sourceViewController]; 
    NMNote *note = source.note; 

    if (source.adding && note != nil) { 
     [self.notes addObject:note]; 
     [self.tableView reloadData]; 
    } 

} 
在NMCreateNotesViewController.m

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    // 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]; 

    if (self.note != nil) 
    { 
     self.originalContent=self.note.content; 
     self.textField.text=self.note.content; 
    } 
} 

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if (sender != self.saveButton) 
    { 
     if (self.note != nil) 
     { 
      self.note.content=self.originalContent; 
     } 
    } 
    else 
    { 
     if (self.note == nil) 
     { 
     if (self.textField.text.length > 0) { 
     self.note = [[NMNote alloc] init]; 
     } 
     self.note.content = self.textField.text; 
    } 
} 
+0

谢谢:)如果我在tableviewcontroller中有一个加号按钮来创建一个新音符,我不能说,如果一个segue从加号按钮中执行它是一个新音符,并且如果它来自单元格选择按钮它不是一个新的?打算加入布尔?只是问@ Paulw11 – Joe

+0

“添加”是因为你知道当unwind segue触发时要做什么 - 如果添加你需要将它添加到数组中,否则它已经在数组中。您可以将标志存储在您的tableview控制器中而不是创建视图控制器中,但在创建中使用它可以选择在该视图中定制事物,例如按照“添加”或“保存”按钮模式 – Paulw11

相关问题