如何创建一个segue以从表格视图回退到编辑模式?

时间:2014-04-01 22:58:44

标签: ios objective-c xcode uitableview

也许我没有在标题中问它,对不起,我是初学者所以我会解释自己:

我有2个屏幕: 1.创建笔记 - 此屏幕具有视图控制器,TextView和导航(带创建/取消)。 2.备注页面 - 此屏幕具有表格视图控制器和带加号按钮的导航。 (非常类似于苹果笔记应用程序)

我希望在表格视图中,每当我点击一个单元格时,它会将我带回到此笔记的可编辑页面...

所以我添加了一个从单元格到笔记页面的推送segue,每次我点击一个单元格,它都会打开一个新的笔记页面...... 所以我知道我在这里遗漏了一些东西,如果能帮助我弄清楚,我会非常感激

这是我的表视图控制器.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

这是我的视图控制器(创建笔记).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

1 个答案:

答案 0 :(得分:1)

@jeely已接近,但您需要做的是,创建一个从viewControllerviewController的segue。

在tableView委托didSelectRowAtIndexPath中,您将预先形成segue:

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

因为您想将注释传递给下一个控制器,您需要在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属性。