gpt4 book ai didi

ios - 如何创建一个 segue 以从 TableView 推回编辑模式?

转载 作者:行者123 更新时间:2023-11-29 12:49:22 25 4
gpt4 key购买 nike

也许我没有在标题中问对,对此感到抱歉,我是初学者所以我会解释自己:

我有 2 个屏幕:1. 创建笔记 - 这个屏幕有一个 View Controller ,TextView 和导航(创建/取消)。2. 注释页面——这个屏幕有一个表格 View Controller 和一个带加号按钮的导航。(非常类似于苹果笔记应用程序)

我希望在表格 View 中,每当我单击一个单元格时,它都会带我回到该笔记的可编辑页面...

所以我添加了一个从单元格到笔记页面的推送 segue,每次我点击一个单元格时它都会打开一个新的笔记页面......所以我知道我在这里遗漏了一些东西,如果你能帮我解决这个问题,我将不胜感激

这是我的 TableView Controller .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;
}

@end

这是我的 View Controller (创建笔记).m 文件:

#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

最佳答案

@jeely 很接近,但您需要做的是,创建从 viewControllerviewController 的转场。

在 tableView 委托(delegate) didSelectRowAtIndexPath 中,您将执行 segue:

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

因为您想将注释传递给下一个 Controller ,所以您需要在 prepareForSegue 方法中执行此操作:

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

最后,noteToDisplay 只是一个 NSString 属性,您将在执行 segue 后将其设置为 textView 属性。

关于ios - 如何创建一个 segue 以从 TableView 推回编辑模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22798498/

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