简体   繁体   中英

how to store old value and new value in same Nsmutablearray in ios

hi i am new in iOS developer.

i have UITableView and UITextField . I set some different value in textfield and each textfield's value store in NSmutablearray than i display this array in next screen. Displayed array value is perfect. But my problem is when i switch back to the UITableView screen and than again set new value in UITextField and these UITextField value store in NSmutablearray .but problem is new value is override to old value. but i want both value store in NSArray .

here my screen shot

在此处输入图片说明 在此处输入图片说明

summaryvc display perfect data.but when i back to the screen and select other item than click the placeorder.but problem is we cant get old selected item in summary screen.

here is button action code to store the array in Nsuserdefault. global.filteredArray is my nsmutablearray.dic value is my dictionary.

this is my array value

{
        counts = 1;
        "m_drycleaning_price" = 12;
        "m_id" = 3;
        "m_imagename" = d;
        "m_iron_price" = 16;
        "m_name" = "Coat(Man)";
        "m_wash_price" = 14;
        "m_washiron_price" = 13;
        total = 12;
    }









AppDelegate *myAppDelegate = [[UIApplication sharedApplication] delegate];
                             [myAppDelegate.arrayCounts  addObject:dictValues];
myAppDelegate.arrayCounts[indexPath.row][@"counts"] = [NSString stringWithFormat:@"%d",(int)value];
    myAppDelegate.arrayCounts[indexPath.row][@"total"] = [NSString stringWithFormat:@"%d",(int)y];


    - (IBAction)placeorder:(id)sender
         {
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

            [userDefaults setObject:global.filteredArray forKey:@"def_orderarraylist"];

            NSMutableArray *qualifiersArray = [NSMutableArray arrayWithArray:[userDefaults objectForKey:@"def_orderarraylist"]];

        NSLog(@"qualifier array=%@",qualifiersArray);

        [userDefaults synchronize];

        } 

please help!

You must declare your array on class rather than on your method and you'll be fine.

You could do something like:

-(void)viewDidLoad{
    array = [[NSMutableArray alloc] init];
}

OR

Use a NSUserDefaults and it will stay until the app is deleted.

If you have two screens you could simply pass the array reference to your second screen if you want to edit your array from there.

FirstViewController:

Setup your array:

SecondViewController *svc= [[SecondViewController alloc] initWithNibName:@"secondviewcontroller" bundle:nil];
svc.firstViewArray=secondViewArray;

SecondViewController:

@property(nonatomic,retain)NSMutableArray *secondViewArray;

I can see you are using global array to store data to NSUserDefaults. While doing this you need to make sure this points:

  1. First thing you need to do is while Adding data to global array, First take previous data from NSUserDefaults & add it to global array.
  2. You need to make sure every time while you add new data to global array you have added previous data to global array from NSUserDefaults
  3. One more thing you need to make sure it global array should be NSMutable array & while you initialize this array, after that Add previous values from NSUserDefaults in it.

Hope it will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM