如何创建转场以从表视图推回编辑模式? [英] 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:
我有两个屏幕:1. 创建笔记 - 此屏幕有一个视图控制器、TextView 和导航(带创建/取消).2. 笔记页面——这个屏幕有一个表格视图控制器和一个带有加号按钮的导航.(非常类似于苹果笔记应用)
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...
所以我添加了一个从单元格到笔记页面的推送segue,每次我点击一个单元格时它都会打开一个新的笔记页面......所以我知道我在这里遗漏了一些东西,如果你能帮我弄清楚,我将不胜感激
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
这是我的表视图控制器 .m 文件:
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
这是我的视图控制器(创建笔记).m 文件:
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
推荐答案
@jeely
很接近但是你需要做的是,创建一个从 viewController
到 <代码>视图控制器代码>.
@jeely
was close but what you need to do is , create a segue from viewController
to viewController
.
在 tableView 委托 didSelectRowAtIndexPath
中,您将执行转场:
In the tableView delegate didSelectRowAtIndexPath
you would preform the segue:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:@"yourSegue" sender:sender];
}
因为您想将注释传递给下一个控制器,所以您需要在 prepareForSegue
方法中执行此操作:
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;
}
}
最后,noteToDisplay
只是一个 NSString 属性,一旦执行 segue
,您将设置为 textView 属性.
Finally noteToDisplay
is just an NSString property that you will set to the textView property once the segue
is performed.
这篇关于如何创建转场以从表视图推回编辑模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!