0

我想用一个按钮将所有内容保存在 UITableView 控制器中,我不知道该怎么做。我知道它有一种保存方法,但是当您按回应用程序的主屏幕并返回视图控制器时,文本消失了,或者当我删除某些内容时它仍然存在。

我的 .m 文件中的代码

#import "HomeWorkViewController.h"

@interface HomeWorkViewController ()

@end

@implementation HomeWorkViewController
@synthesize adView;
@synthesize myTableView, numbers;

-(void) viewDidLoad
{
   adView.delegate=self;
   [super viewDidLoad];
   self.navigationItem.leftBarButtonItem = self.editButtonItem;
    // check here if key exists in the defaults or not, if yes the retrieve results in array
   if([[NSUserDefaults standardUserDefaults] objectForKey:@"numberArray"] != nil) {
       self.numbers = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"numberArray"]];
    }

    //Register for the notification when user go to background or minimize the app, just save the array objects in the defaults
    [[NSNotificationCenter defaultCenter]   addObserver:self
                                            selector:@selector(appWillGoToBackground:)
                                            name:UIApplicationWillResignActiveNotification object:[UIApplication sharedApplication]];

    //Add the Add button
    UIBarButtonItem * addButton = [[UIBarButtonItem alloc]
                                   initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                   target: self action: @selector(insertNewObject)];
    self.navigationItem.rightBarButtonItem = addButton;
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.myTableView setEditing:editing animated:animated];
}

-(void)appWillGoToBackground:(NSNotification *)note {
    NSLog(@"terminate");
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setObject:self.numbers forKey:@"numberArray"];
    [defaults synchronize];
}

-(void)insertNewObject{
    //Display a UIAlertView
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter HomeWork" message: @"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //Only perform the following actions if the user hits the ok button
    if (buttonIndex == 1)
    {
        NSString * tmpTextField = [alertView textFieldAtIndex:0].text;
        if(!self. numbers){
            self.numbers = [[NSMutableArray alloc]init];
        }
        [self.numbers insertObject:tmpTextField atIndex:0];
        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.myTableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

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

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cell";

    UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
                                        reuseIdentifier: cellIdentifier];
    }
    cell.textLabel.text = [self.numbers objectAtIndex:indexPath.row];
    return cell;
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (editingStyle == UITableViewCellEditingStyleDelete)
   {
      //remove our NSMutableArray
      [self.numbers removeObjectAtIndex:indexPath.row];
      //remove from our tableView
      [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
   }
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    adView.hidden=FALSE;
    NSLog(@"Has ad, showing");
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    adView.hidden=TRUE;
    NSLog(@"Has no ads, hiding");
}

-(void)dealloc
{
    [adView release];
    [super dealloc];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}
@end
4

1 回答 1

0

看起来您只是在应用程序进入后台时才保存数据。如果您想立即保存数据而不是仅在应用关闭时保存数据,只需将数组写入 alertView:clickedButtonAtIndex 中的 NSUserDefaults:

于 2013-04-07T17:51:44.663 回答