How add values from text field / label to an array

2019-09-06 09:58发布

问题:

I have a text field In that user can enter values from 1-10

at the bottom of the text field i have button.

i need to add the values in text filed to array when user clicks done button

for this i wrote code like this.

- (void)viewDidLoad
{
    [super viewDidLoad];
    PainlevelsArray = [[NSMutableArray alloc] init]; //PainlevelsArray declared in .h

}


-(IBAction)doneClick:(id)sender
{
    [PainlevelsArray addObject:txtField.text ];

    // [PainlevelsArray insertObject:rateLabel.text atIndex:0 ];
    NSLog(@"PainlevelsArray *** %@",PainlevelsArray);

   [self.navigationController popToRootViewControllerAnimated:YES];

}

But every time array displays only the recently added one value only in log when button clicks, like this

Output on Console

========================

2012-05-07 10:11:15.689 ESPT[796:10d03] PainlevelsArray *** (
    2
)
2012-05-07 10:11:27.984 ESPT[796:10d03] PainlevelsArray *** (
    4
)

My array gets empty every time view loads, and adde a fresh entry when button clicks But i need like this

2012-05-07 10:11:27.984 ESPT[796:10d03] PainlevelsArray *** (
        4,
        5,
        2,
        6,
        8,
    )

All the velues enters are adde/inserted to the existing array.

Thanks in advance

in ExerciseStartViewController

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 ExerciseResult *result = [[ExerciseResult alloc] init];
            [self.navigationController pushViewController:result animated:YES];
            [result release];
    }

in current/ ExerciseResult viewcontroller

- (void)viewDidLoad
{
    [super viewDidLoad];
    PainlevelsArray = [[NSMutableArray alloc] init]; //PainlevelsArray declared in .h

}


-(IBAction)doneClick:(id)sender
{
    [PainlevelsArray addObject:txtField.text ];

    // [PainlevelsArray insertObject:rateLabel.text atIndex:0 ];
    NSLog(@"PainlevelsArray *** %@",PainlevelsArray);

   [self.navigationController popToRootViewControllerAnimated:YES];

}

回答1:

You need to use a global array.

Best possible solution is declare an array in AppDelegate.h file and alloc-init this array in applicationdidfinishlaunch method.

AppDelegate *app =  (AppDelegate*)[[UIApplication sharedApplication] delegate];
[app.arrMutable addObject:textField.text ]; 

Use UserDefaults: These are used to save your application data in key-value format. The values/objects stored as userdefaults always remain in application, until you have not removed it from sandbox. You can use following code to store an array in userdefaults.

[[NSUserDefaults standardUserDefaults] setObject:app.arrMutable forKey:@"Global_Array_Key"];

In AppDelegae applicationDidFinishLuanch method you can retrieve this array from userdefaults using:

self.arrMutable = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:@"Global_Array_Key"]];


回答2:

You need to declare and initialize PainlevelsArray globally, i.e. in AppDelegate or separate class of shared instance, where memory has been assigned once and it won't over write its data even though any number of time you access the said class and so that it will continuously inserts new value into the array and you will get the desired result.



回答3:

ExerciseResult *result = [[ExerciseResult alloc] init];
[self.navigationController pushViewController:result animated:YES];
[result release];

I think you might be fallow this type of code in you action for coming to this view. Instead of this you have to fallow to take as global ExerciseResult *result

in .h

ExerciseResult *result;

in .m

in viewDidLoad

result = [[ExerciseResult alloc] init];

in you action method you will fallow

[self.navigationController pushViewController:result animated:YES];

you will follow like that

I hope it will helps you



回答4:

Declare in Delegate.h page

  @interface AppDelegate : NSObject <UIApplicationDelegate>
  {
       NSMutableArray *commString;
  }
  @property (nonatomic, retain) NSMutableArray *commString;
  @end

Delegate.m page

  @sythesize commString;

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
          commString = [[NSMutableArray alloc] init];
          // some code.....
 }

In your Button view

 -(IBAction)doneClick:(id)sender
{
NSMutableArray *mut=[[NSMutableArray alloc] initWithObjects:txtField.text,nil];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.commString=mut;

   // some code....
}

Declare this code where u want your array, you can easily get ur array.

   AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
   NSLog(@"commString=%@",appDelegate.commString);


回答5:

You use this line of code when you click just write add this line.

[array addobject:textfield.text];

use this only.

[PainlevelsArray addObject:txtField.text]


回答6:

You should use properties for an array in this situation.

@property (nonatomic, strong) NSMutableArray *painlevelsArray;

Initialize the array

NSMutableArray *array = [[NSMutableArray alloc] init];
self.painlevelsArray = array;

Then add your string to the array after each button click

[self.painlevelsArray addObject:[NSString stringWithFormat:@"%@",self.textField.text]];