如何在 segue 中传递对象而不是对象的细节? [英] How to pass an object in a segue rather that a detail of an object?

查看:22
本文介绍了如何在 segue 中传递对象而不是对象的细节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的笔记应用,其中只有 2 个视图控制器:

I have a simple notes app where I have just 2 view controllers:

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

在表视图控制器中,我有一个从单元格返回到创建页面的segue,用户可以在其中编辑此特定单元格中的注释.

In the table view controller I have a segue from a cell back to the creation page where a user can edit the note in this specific cell.

但我的问题是,当我对某个单元格(注释)进行编辑时,我正在使用我编辑的内容创建一个新注释...

But my problem is that when I'm preforming editing to a certain cell(note) I'm creating a new note with the content of what I edited...

所以不是在 prepareForSegue 方法中传递便笺内容,我需要传递便笺对象...

So instead of passing the note content in the prepareForSegue method I need to pass the note object...

我该怎么做?

这是我的课程:

NM注意:(正确地只包含*content的属性,稍后会添加更多行为)

NMNote: (correctly just containing a property of *content, will add more behaviour later)

#import <Foundation/Foundation.h>

@interface NMNote : NSObject

@property (strong, nonatomic) NSString *content;


@end

NMCreateNotesViewController.h:

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:

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:

NMNotesListViewController.h:

#import <UIKit/UIKit.h>

@interface NMNotesListViewController : UITableViewController

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

@end

NMNotesListViewController.m:

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

这是屏幕流程:

初始视图是这个表视图:

the initiate view is this table view:

现在有你写笔记的TextView:

Now there is the TextView where you write the note:

现在,保存笔记后,您将返回到第一个屏幕.然后您可以点击填充的单元格,您将返回到此屏幕(带有 TextView 的屏幕),以便您可以对其进行编辑.但它不会编辑它,而是用编辑过的内容创建一个新的.像这样:

Now, after you save a note, you go back to the first screen. and then you can tap on a populated cell and you will segue back to this screen (the one with the TextView) so you can edit it. But instead of editing it, it will create a new one with the edited content. like this:

请在这里完成我的任务,我将不胜感激.谢谢!

Please, would appreciate any help here to accomplish my task.. Thanks!

推荐答案

当您将注释传递给 NMCreateNotesViewController 时,您需要做的事情是区分编辑和添加操作,以便在您返回表格时视图,您可以用新编辑的条目替换旧条目,也可以添加新条目.

The thing you need to do when you pass the note to the NMCreateNotesViewController, is to differentiate between an edit and an add action so when you came back to the table view, you can either replace the old entry with the new edited one, or add a new entry.

我的处理方式是有两个 segue,一个来自 + 按钮(我将其称为addSegue"),另一个来自表格视图单元格(称为editSegue").我还将在列表控制器中创建一个属性来保存已编辑行的值,或者将其设置为 -1 之类的内容以指示它是一个新笔记.像这样,

The way I would approach this is to have two segues, one from the + button (I'll call it "addSegue") and one from the table view cell (call it "editSegue"). I would also create a property in the list controller to hold the value of the edited row, or set it to something like -1 to indicate it's a new note. Something like this,

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

NMCreateNotesViewController 中的 prepareForSegue 方法与您在问题中的方法相同.你可以去掉passedInString 属性,因为我们传入的是整个note 对象.在列表控制器的 unwind 方法中,你会这样做,

The prepareForSegue method in the NMCreateNotesViewController would be the same as you have in your question. You can get rid of the passedInString property since we're passing in the entire note object instead. In the unwind method in the list controller, you would do this,

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

这篇关于如何在 segue 中传递对象而不是对象的细节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆