简体   繁体   English

如何在iOS的同一Nsmutablearray中存储旧值和新值

[英]how to store old value and new value in same Nsmutablearray in ios

hi i am new in iOS developer. 嗨,我是iOS开发人员的新手。

i have UITableView and UITextField . 我有UITableViewUITextField I set some different value in textfield and each textfield's value store in NSmutablearray than i display this array in next screen. 与在下一个屏幕中显示此数组相比,我在textfield中设置了一些不同的值,并且在NSmutablearray设置了每个textfield的值存储。 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. 但是我的问题是当我切换回UITableView屏幕并再次在UITextField设置新值并将这些UITextField值存储在NSmutablearray问题是新值被覆盖为旧值。 but i want both value store in NSArray . 但我想两个值都存储在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. summaryvc显示完美的数据。但是当我返回到屏幕并选择其他项目而不是单击placeorder。但是问题是我们无法在摘要屏幕中获得旧的选定项目。

here is button action code to store the array in Nsuserdefault. 这是用于将数组存储在Nsuserdefault中的按钮操作代码。 global.filteredArray is my nsmutablearray.dic value is my dictionary. global.filteredArray是我的nsmutablearray.dic值是我的字典。

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. 使用NSUserDefaults ,它将一直保留到删除该应用程序为止。

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: FirstViewController:

Setup your array: 设置阵列:

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

SecondViewController: SecondViewController:

@property(nonatomic,retain)NSMutableArray *secondViewArray;

I can see you are using global array to store data to NSUserDefaults. 我可以看到您正在使用全局数组将数据存储到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. 您需要做的第一件事是在将数据添加到全局数组中时,首先从NSUserDefaults中获取先前的数据并将其添加到全局数组中。
  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 您需要确保每次将新数据添加到全局数组时,都已将以前的数据从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. 您还需要确保全局数组应该是NSMutable数组,并在初始化此数组时,之后在其中添加NSUserDefaults中的先前值。

Hope it will help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在iOS中合并具有相同值的NSMutableArray中的2个NSDictionaries? - How to merge 2 NSDictionaries in NSMutableArray having same value in iOS? 如何从ViewControllers在NSMutableArray中存储值 - How to store value in NSMutableArray from ViewControllers 如何在循环中将NSDictionary的值存储到NSMutableArray? - How to store Value of NSDictionary to NSMutableArray in loop? 如何在ios中使用按钮单击事件nsmutablearray添加json和stepper值存储 - how to add json and stepper value store in using button click event nsmutablearray in ios ios中的NSMutableArray字符串数组值 - string array value to NSMutableArray in ios iOS 7:自定义UILabel新值会在旧值上打印 - iOS 7: Custom UILabel new value prints on old value 如何在iOS中的字符串格式中将文本字段值加载到nsmutablearray中 - how to load textfield value in to nsmutablearray in string formate in ios 如何删除具有相同属性值但在NSMutableArray中具有相同属性值的所有对象 - How to remove all objects with the same property value but one in NSMutableArray swift无法在NSUserdefault中存储NSMutableArray的值 - Can't store value of NSMutableArray in NSUserdefault in swift 如何获取 NSPropertyDescription 属性的新旧值 - How to get old and new value of property of NSPropertyDescription
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM