Joe
Joe

Reputation: 2633

How to to create a segue to push back to editing mode from a table view?

Maybe I didn't ask it right in the title, sorry for this, i'm a beginner so I will explain myself:

I have 2 screens: 1. Create notes - this screen have a view controller, TextView and navigation(with create/cancel). 2. Notes page - this screen have a table view controller and a navigation with a plus button. (very similar to apple notes app)

I want that in the table view, whenever I click a cell it will take me back to the editable page of this note...

So I added a push segue from the cell to the notes page, and every time I'm clicking on a cell it opens a NEW note page... So I know i'm missing something here and would really appreciate if you can help me figure it out

This is my table view controller .m file:

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

@end

And this is my view controller (create notes) .m file:

#import "NMCreateNotesViewController.h"

@interface NMCreateNotesViewController ()

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

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

@end

@implementation NMCreateNotesViewController

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];

}

- (void)keyboardWasShown:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGRect bkgndRect = self.textField.superview.frame;
    bkgndRect.size.height += kbSize.height;
    [self.textField.superview setFrame:bkgndRect];
    [self.textField setContentOffset:CGPointMake(0.0, self.textField.frame.origin.y-kbSize.height) animated:YES];
}


- (void) keyboardWillBeHidden: (NSNotification *) aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;

    self.textField.contentInset = contentInsets;

    self.textField.scrollIndicatorInsets = contentInsets;
}


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if (sender != self.createButton) 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];
    // Do any additional setup after loading the view.
}

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

@end

Upvotes: 1

Views: 266

Answers (1)

meda
meda

Reputation: 45500

@jeely was close but what you need to do is , create a segue from viewController to viewController.

In the tableView delegate didSelectRowAtIndexPath you would preform the segue:

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   [self performSegueWithIdentifier:@"yourSegue" sender:sender];
 }

Because you want to pass the note to the next controller you will need to do that in the prepareForSegue method:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"yourSegue"])
    {  
       //get the note
       NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
       NMNote *note = [self.notes objectAtIndex:indexPath.row];
       //set the note
       NMCreateNotesViewController *createVC = (NMCreateNotesViewController*)segue.destinationViewController;
       createVC.noteToDisplay = note.content;
    }
}

Finally noteToDisplay is just an NSString property that you will set to the textView property once the segue is performed.

Upvotes: 1

Related Questions