0

Xcode でのプログラミングは初めてで、アプリ (ToDo リスト) で問題が発生しています。すべて正常に動作しますが、アプリを終了すると (最小化されません)、メイン ビュー コントローラーは内容を保存しません (ToDo リストがある場合の問題)。ここで、終了時にビュー コントローラーの状態を保持するために実装する必要があるコードと、その場所を知りたいと思います。(アプリ デリゲートまたはメイン ビュー コントローラー ウィンドウ)

.m ファイル:

@implementation RHTaskListViewController

@synthesize tasks = _tasks;

-(id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self)
    {
        //custom
    }
    return self;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tasks = [[NSMutableArray alloc] init];
}




- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

//Segue from Add task to task list
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"AddTaskSegue"])
    {

        UINavigationController *navCon = segue.destinationViewController;

        RHAddTaskViewController *addTaskViewController = [navCon.viewControllers objectAtIndex:0];

        addTaskViewController.taskListViewController = self;
    }


    else if ([segue.identifier isEqualToString:@"EditDoneTaskSegue"] || [segue.identifier isEqualToString:@"EditNotDoneTaskSegue"])
    {
        RHEditTaskViewController *editTaskViewController = segue.destinationViewController;
        editTaskViewController.task = [self.tasks objectAtIndex:self.tableView.indexPathForSelectedRow.row];
    }
}
//Segue from Add task to task list


//Move Items
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath         toIndexPath:(NSIndexPath *)toIndexPath
{
    RHTask *movedTask = [self.tasks objectAtIndex:fromIndexPath.row];
    [self.tasks removeObjectAtIndex:fromIndexPath.row];
    [self.tasks insertObject:movedTask atIndex:toIndexPath.row];
}

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//Move Items


//Delete Items
-(void)tableView:(UITableView *)tableView commitEditingStyle:    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self.tasks removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]     withRowAnimation:UITableViewRowAnimationFade];
    }

    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {

    }
}
//Delete Items


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}    

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tasks.count;
}



-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *NotDoneCellIdentifier = @"NotDoneTaskCell";
static NSString *DoneCellIdentifier = @"DoneTaskCell";

RHTask *currentTask = [self.tasks objectAtIndex:indexPath.row];

NSString *cellIdentifier = currentTask.done ? DoneCellIdentifier : NotDoneCellIdentifier;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
//Saving the tasks

//[[NSUserDefaults standardUserDefaults] setString:saveTask forKey:@"taskSaved"];


//Saving the tasks

cell.textLabel.text = currentTask.name;


return cell;
}



#pragma mark - IBActions

-(void)editButtonPressed:(id)sender
{
    self.editing = !self.editing;
}

@end

.h ファイル:

#import <UIKit/UIKit.h>

@interface RHTaskListViewController : UITableViewController

@property (nonatomic, strong) NSMutableArray *tasks;

-(IBAction)editButtonPressed:(id)sender;

@end
4

2 に答える 2

2

NSUserDefaultsあなたの目標を達成するために使用できます。

このチュートリアルのリンクを使用してください

メインコード:

保存する場合:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"key1" forKey:@"todo1"];
[defaults synchronize]; // do NOT forget this line!

取得する場合:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *todo1 = [defaults objectForKey:@"key1"];

、、、、およびプロトコルに準拠するすべてNSArrayのオブジェクトを保存できることに注意してください。NSDictionaryNSStringNSNumberNSCoping

于 2014-07-29T06:37:58.507 に答える
1

gran33 が既に述べたようNSUserDefaultsに、アプリ データの保存に使用できます。

Todo リストが単なるタスクのリストよりも複雑な場合 (つまり、各タスクに時間、説明、場所などのプロパティが含まれている場合)、コア データを使用してデータを保存することをお勧めします。CoreDataのチュートリアルについては、こちらをご覧ください。

于 2014-07-29T06:38:31.453 に答える