如何在对象中添加数据并将数据保存到数组中?

时间:2017-08-31 15:35:28

标签: ios objective-c uitableview

所以我正在做一个小小的项目,我想做的是创建一个类似于笔记应用程序的东西。我正在做的是允许用户在文本字段中键入其注释的标题,然后将其添加到UITableViewCell,然后当他们点击该单元格时,它将他们带到另一个视图,在那里他们可以编辑他们的标题,保存它和然后将它们返回到原始视图,但使用与最初输入的标题不同的标题。

以下是我目前的问题

1)关闭应用后,如何保存新标题? (当我添加新标题然后重新启动模拟器时,UITableView为空)

2)用户编辑标题后,如何永久更改UITableViewCell? (标题文本进入文本字段,但是当我编辑标题并点击保存并返回原始视图控制器时,原始标题仍然存在)

这是第一个视图控制器的代码:

#import "TestViewController.h"

@interface TestViewController (){
}

@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _array = [[NSMutableArray alloc] initWithObjects:@"First Note", nil];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)btn:(id)sender {
    [_array insertObject:self.fld.text atIndex:0];
    [_tableView reloadData];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [_array count];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (cell == nil){
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    cell.textLabel.text = [_array objectAtIndex:indexPath.row];

    return cell;
}

-  (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if ([segue.identifier isEqualToString:@"segue1"]){
        Test1ViewController *vc = [segue destinationViewController];
       Connecter *connectorClass = [[Connecter alloc] init];
        connectorClass.stringToPass = self.array[self.tableView.indexPathForSelectedRow.row];
        vc.connectorClass = connectorClass;
    }

}

继承我的第二个视图控制器的代码:

#import "Test1ViewController.h"

@interface Test1ViewController ()

@end

@implementation Test1ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _label.text = _connectorClass.stringToPass1;
    _textView.text = _connectorClass.stringToPass;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)button1:(id)sender {
    [self performSegueWithIdentifier:@"segue2" sender:self];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"segue2"]){
    Test2ViewController *vc1 = [segue destinationViewController];
    Connecter *connectorClass = [[Connecter alloc] init];
    connectorClass.stringToPass1 = _textView.text;
    vc1.connectorClass = connectorClass;
    }
}

这是我用作中间人的第三个视图控制器,用于将信息从testviewcontroller传递到test1viewcontroller

#import <UIKit/UIKit.h>

@interface Connecter : UIViewController

@property (nonatomic, strong) Connecter *connectorClass;
@property (nonatomic, strong) NSString *stringToPass;
@property (nonatomic, strong) NSString *stringToPass1;

@end

0 个答案:

没有答案