gpt4 book ai didi

ios - 如何在 segue 中传递对象而不是对象的细节?

转载 作者:行者123 更新时间:2023-11-29 03:05:04 25 4
gpt4 key购买 nike

我有一个简单的笔记应用程序,其中只有 2 个 View Controller :

  1. TableView Controller - 列出所有笔记。
  2. View Controller - 创建新笔记。

在 TableView Controller 中,我有一个从单元格返回到创建页面的 segue,用户可以在该页面编辑此特定单元格中的注释。

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

因此,我需要传递笔记对象,而不是在 prepareForSegue 方法中传递笔记内容...

我该怎么做?

这是我的类(class):

NMNote:(正确地只包含 *content 的属性,稍后将添加更多行为)

#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

这是屏幕流程:

初始 View 是这个 TableView :

enter image description here

现在有你写笔记的 TextView:

enter image description here

现在,保存笔记后,您将返回到第一个屏幕。然后你可以点击一个填充的单元格,你将返回到这个屏幕(带有 TextView 的那个),这样你就可以编辑它了。但是它不会编辑它,而是会使用编辑后的内容创建一个新的。像这样:

enter image description here

拜托,我将不胜感激任何帮助来完成我的任务..谢谢!

最佳答案

当你将笔记传递给 NMCreateNotesViewController 时,你需要做的是区分编辑和添加操作,这样当你返回到 TableView 时,你可以用新编辑的条目替换旧条目, 或添加新条目。

我采用的方法是使用两个转场,一个来自 + 按钮(我称之为“addSegue”),另一个来自表格 View 单元格(称之为“editSegue”)。我还会在列表 Controller 中创建一个属性来保存编辑行的值,或者将其设置为类似 -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;
}

NMCreateNotesViewController 中的 prepareForSegue 方法与您在问题中使用的方法相同。您可以去掉 passedInString 属性,因为我们传递的是整个笔记对象。在列表 Controller 的 unwind 方法中,您可以这样做,

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

关于ios - 如何在 segue 中传递对象而不是对象的细节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22874470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com